jfrog / project-examples

Small projects in universal build ecosystems to configure CI and Artifactory
1.01k stars 2.39k forks source link

maven-container settings.xml #114

Open sushant-pradhan opened 6 years ago

sushant-pradhan commented 6 years ago

Maven running in a container/jenkins slave is not picking the settings.xml file present under $MAVEN_HOME/conf. Tried both the options as described under this example the only way to make the plugin pick the global settings is to pass it as an argument: artifactoryMaven.run pom: "pom.xml", goals: "install -s /usr/share/java/maven-3/conf/settings.xml"

Should it not be automatically picking up the global settings.xml?

eyalbe4 commented 6 years ago

@an-sush, I've just tried this with a Jenkins slave running inside a docker container, and the setting.xml is used as expected. Could it be that your Jenkins job is using a different MAVEN_HOME and therefore maven is not searching the settings.xml where you placed it? As an additional troubleshooting step, you can try placing the setting.xml inside the .m2 directory, since maven will pick it also from there.

sushant-pradhan commented 6 years ago

@eyalbe4

I am using alpine slave and placed my settings.xml under the maven home:

COPY settings.xml /usr/share/java/maven-3/conf/settings.xml

When I run a raw maven command, it works fine (picks from the repos defined in my settings.xml copied above): mvn clean compile

I then use this maven to run my artifactory plugin:

withEnv(['MAVEN_HOME=/usr/share/java/maven-3']) {
        def buildInfo = artifactoryMaven.run pom: "pom.xml", goals: "install -DskipTests"
        artifactoryMaven.deployer.deployArtifacts buildInfo
    }

The above snippet doesn't pick my settings.xml. I tries to download from maven central repo

But the following change makes the plugin to pick correct settings.xml:

withEnv(['MAVEN_HOME=/usr/share/java/maven-3']) {
        def buildInfo = artifactoryMaven.run pom: "pom.xml", goals: "install -DskipTests -s /usr/share/java/maven-3/conf/settings.xml"
        artifactoryMaven.deployer.deployArtifacts buildInfo
    }
eyalbe4 commented 6 years ago

@an-sush, Did you configure a resolver on the artifactoryMaven instance? You shouldn't if you'd like to use the settings.xml. As a troubleshooting step, can you try running the same script on the master or on a non docker agent? Can you please share your entire pipeline script?

sushant-pradhan commented 6 years ago

Hello,

The maven settings which is embedded in my slave image under MAVEN_HOME/conf:

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <id>central</id>
      <username>${env.ARTIFACTORY_USER}</username>
      <password>${env.ARTIFACTORY_PASSWORD}</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>${env.ARTIFACTORY_USER}</username>
      <password>${env.ARTIFACTORY_PASSWORD}</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>maven-prod</name>
          <url>https://aaa.bbb/artifactory/maven-prod</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>maven-dev</name>
          <url>https://aaa.bbb/artifactory/maven-dev</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>maven-prod</name>
          <url>https://aaa.bbb/artifactory/maven-prod</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>maven-dev</name>
          <url>https://aaa.bbb/artifactory/maven-dev</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

Project pom (note it has a parent dependency from artifactory):

<parent>
        <groupId>bbb.aaa</groupId>
        <artifactId>proj-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

It is verified that this pom/jar is present in Artifactory's dev repository

And here is my pipeline script:

def server = Artifactory.newServer url: "https://aaa.bbb/artifactory", username: "someuser", password: "password"
    def artifactoryMaven = Artifactory.newMavenBuild()
    artifactoryMaven.resolver server: server, releaseRepo: 'maven-prod', snapshotRepo: 'maven-dev'
    artifactoryMaven.deployer server: server, releaseRepo: 'maven-prod', snapshotRepo: 'maven-dev'
    withEnv(['MAVEN_HOME=/usr/share/java/maven-3']) {
        def buildInfo = artifactoryMaven.run pom: "pom.xml", goals: "install -DskipTests -s /usr/share/java/maven-3/conf/settings.xml"
        artifactoryMaven.deployer.deployArtifacts buildInfo
    }

Please note that -s /usr/share/java/maven-3/conf/settings.xml is a hack to make the build work. Ideally it should work without passing the settings argument since we are setting the maven resolver and server is pointing to correct instance

eyalbe4 commented 6 years ago

@an-sush, Can you please try to comment out the artifactoryMaven.resolver configuration? This line tells your build to ignore the Artifactory servers defined in your settings.xml for dependencies resolution and use the repository defined by artifactoryMaven.resolver instead.

sushant-pradhan commented 6 years ago

Here is my updated pipeline:

def server = Artifactory.newServer url: "https://aaa.bbb/artifactory", username: "someuser", password: "password"
    def artifactoryMaven = Artifactory.newMavenBuild()
    // artifactoryMaven.resolver server: server, releaseRepo: 'maven-prod', snapshotRepo: 'maven-dev'
    artifactoryMaven.deployer server: server, releaseRepo: 'maven-prod', snapshotRepo: 'maven-dev'
    withEnv(['MAVEN_HOME=/usr/share/java/maven-3']) {
        def buildInfo = artifactoryMaven.run pom: "pom.xml", goals: "install -DskipTests"
        artifactoryMaven.deployer.deployArtifacts buildInfo
    }

And still I see build errors:

[FATAL] Non-resolvable parent POM for bbb.aaa:proj-parent:0.0.1-SNAPSHOT: Could not find artifact bbb.aaa:proj-parent:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM @ line 14, column 10

eyalbe4 commented 6 years ago

@an-sush, Could it be that you affected by HAP-192? This fix hasn't been released yet, but you can try the snapshot version which is included in the ticket comment.

sushant-pradhan commented 6 years ago

i can't actually use the beta version so can't confirm btw that is a really old bug which is still open

eyalbe4 commented 6 years ago

@an-sush, This fix is included in the upcoming official release of the Artifactory Plugin, planned to be released during the next few days.

sushant-pradhan commented 6 years ago

Ok..awaiting the new plugin.