Fix maven central http issue

Starting from January 15, 2020 maven central repository is not supporting access via http://repo1.maven.org/maven2 due to security reasons. Unfortunately under the hood hybris uses ant-maven-task, which is not maintained since 2011 and has hardcoded link to http version of maven central.

Such situation leads to download errors during hybris ant build in case of missing dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[artifact:mvn] Downloading: org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom from repository central at http://repo1.maven.org/maven2
[artifact:mvn] Error transferring file: Server returned HTTP response code: 501 for URL: http://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom
[artifact:mvn] [WARNING] Unable to get resource 'org.apache.maven:apache-maven:pom:3.2.5' from repository central (http://repo1.maven.org/maven2): Error transferring file: Server returned HTTP response code: 501 for URL: http://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom
[artifact:mvn] Downloading: org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom from repository central at http://repo1.maven.org/maven2
[artifact:mvn] Error transferring file: Server returned HTTP response code: 501 for URL: http://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom
[artifact:mvn] [WARNING] Unable to get resource 'org.apache.maven:apache-maven:pom:3.2.5' from repository central (http://repo1.maven.org/maven2): Error transferring file: Server returned HTTP response code: 501 for URL: http://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5.pom
     [null] An error has occurred while processing the Maven artifact tasks.
     [null]  Diagnosis:
     [null] 
     [null] Unable to resolve artifact: Missing:
     [null] ----------
     [null] 1) org.apache.maven:apache-maven:pom:3.2.5
     [null]   Path to dependency: 
     [null]   	1) org.apache.maven:super-pom:pom:2.0
     [null]   	2) org.apache.maven:apache-maven:pom:3.2.5
     [null] 
     [null] ----------
     [null] 1 required artifact is missing.
     [null] 
     [null] for artifact: 
     [null]   org.apache.maven:super-pom:pom:2.0
     [null] 
     [null] from the specified remote repositories:
     [null]   central (http://repo1.maven.org/maven2)
     [null] 
     [null] 

BUILD FAILED
/hybris/bin/platform/build.xml:319: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/mavenTasks.xml:436: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/util.xml:20: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/mavenTasks.xml:438: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/mavenTasks.xml:659: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/mavenTasks.xml:689: The following error occurred while executing this line:
/hybris/bin/platform/resources/ant/mavenTasks.xml:490: Unable to resolve artifact: Missing:
----------
1) org.apache.maven:apache-maven:pom:3.2.5
  Path to dependency: 
  	1) org.apache.maven:super-pom:pom:2.0
  	2) org.apache.maven:apache-maven:pom:3.2.5

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2)

As a possible fix could be added maven mirror to https version for http maven central repository. In AbstractArtifactWithRepositoryTask#getDefaultRemoteRepository is defined default repository with id central. So adding settings.xml with mirror entry will fix the issue:

1
2
3
4
5
6
7
8
9
<settings>
    <mirrors>
        <mirror>
            <id>central.mirror</id>
            <url>https://repo1.maven.org/maven2/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
</settings>

Changing of CI/CD pipeline to add maven settings.xml as local users settings looks overcomplicated and would be better to define settings during hybris ant build.

According to AbstractArtifactTask#initSettings ant-maven-task will lookup settings.xml file in such locations:

  1. $USER_HOME/.ant/settings.xml

  2. $USER_HOME/.m2/settings.xml

  3. $ANT_HOME/etc/settings.xml

For our purposes location 3 looks like the best choice.

Below could be find macrodef to copy maven settings.xml from hybirs config dir into hybris ant dir and remove it on clean. This macrodef should be inserted in buildcallbacks.xml of any extension. Also don’t forget to change yourextension to proper extension name and put settings.xml file in $HYBRIS_CONFIG_DIR/maven/settings.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    <!-- When SAP will fix issue with maven default url both macrodefs and maven settings file could be removed. -->
    <macrodef name="yourextension_before_updateMavenDependencies">
        <sequential>
            <echo message="Add maven settings"/>
            <copy file="${HYBRIS_CONFIG_DIR}/maven/settings.xml" todir="${ant.home}/etc" failonerror="false"/>
        </sequential>
    </macrodef>

    <!-- Clean up custom maven settings -->
    <macrodef name="yourextension_before_clean">
        <sequential>
            <delete failonerror="false" file="${ant.home}/etc/settings.xml"/>
        </sequential>
    </macrodef>

P.S. Don’t forget to change yourextension in macrodef definitions to your real extension name.

P.P.S. Hybris 6.6 Patch 22 has bug in build.xml, which doesn’t pass extension name into build callback to bypass that issue could be used such macrodef:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    <!-- There is a bug in current hybris version build, which doesn't pass extension name during callback execution.
    Macrodef _before_updateMavenDependencies is dirty workaround. When hybris will fix this issue it could be removed
    and proper yourextension_before_updateMavenDependencies would be used.
    -->
    <macrodef name="_before_updateMavenDependencies">
        <sequential>
            <echo message="Add maven settings"/>
            <copy file="${HYBRIS_CONFIG_DIR}/maven/settings.xml" todir="${ant.home}/etc" failonerror="false"/>
        </sequential>
    </macrodef>
comments powered by Disqus