Couldn't transfer dependancies from maven when running from Jenkins - java

I have a script in Java, I could run it with maven commands locally and even in the local Jenkins setup (localhost)
Same set of scripts when I try to run from company jenkins server, it throws an error as this :
[ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.5: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org: unknown error: Unknown host repo.maven.apache.org: unknown error -> [Help 1]
[ERROR]*
Note :
I setup the jenkins server from ansible playbook, and the pipeline script is as follows:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git branch: '*/work', credentialsId: 'git-deploy', url:'<correct git url>'
}
stage('Build') {
// Run the maven build
docker.image("maven:3.2-jdk-8").inside("--privileged=true -e TZ=Asia/Singapore") {
sh 'mvn --version'
sh 'which mvn'
sh "'/usr/bin/mvn' clean test"
}
}
What could be the cause? please help.

It looks like a proxy issue.
Your Maven setup is unable to download the maven-clean-plugin-2.5.jar.
Try to download the jar file manually in the machine where Jenkins is installed. Open the following link in the browser:
http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
If you are able to download it here, then check your browser's proxy and try to use the same proxy in the settings.xml of your maven.
Example:
<proxy>
<id>example-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.example.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>www.google.com|*.example.com</nonProxyHosts>
</proxy>
Check this guide for proxy settings in Maven.

Related

Maven build fail: "invalid target release" only in GitLab CI

Part of my GitLab CI pipeline uses Maven ( mvn test ). Maven fails to build my test project with the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project GenericTests: Fatal error compiling: invalid target release: 13 -> [Help 1]
I did some research and apparently setting JAVA_HOME to the location of JDK 13 would work, and it does, but only when I do it by hand locally via SSH.
In my pipeline, even if I export JAVA_HOME right before running mvn test I still get the same error.
script:
- export JAVA_HOME=/usr/java/jdk-13.0.2
- mvn test
Why does this work differently between GitLab CI and SSH? Could it have to do with the fact that GitLab CI is a non-interactive shell? What can I do to remedy this?
EDIT: I also tried to add a release tag in pom.xml like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>13</release>
</configuration>
</plugin>
but this only resulted in another error during build time:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project GenericTests: Fatal error compiling: invalid flag: --release -> [Help 1]
It turned out to be a mess-up on my part: mvn test was being executed by means of an alias, which was actually sudo mvn test. Hence why the environment variable wasn't being used and why I got Mojo permission denied errors locally when I tried to rerun tests.

Why does Maven Surefire 3 fail to run my Cucumber test in Jenkins with Docker?

I have a Java project with some Cucumber tests and some regular JUnit tests, managed by Maven.
I want to run the tests in Jenkins, using Docker, so I wrote this Jenkinsfile:
pipeline {
agent {
docker {
image 'maven:3.6.1'
}
}
stages {
stage('build') {
steps {
sh 'mvn clean verify -Dmaven.test.failure.ignore=true'
}
post {
success {
junit 'target/surefire-reports/**/*.xml'
}
}
}
}
}
When I run the build, the regular tests pass but the Cucumber tests fail with:
Error Message
URI has a query component
Stacktrace
java.lang.IllegalArgumentException: URI has a query component
It's not until I disable trimStackTrace for maven-surefire-plugin that I also get the details:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
java.lang.IllegalArgumentException: URI has a query component
at java.base/java.io.File.<init>(File.java:427)
at cucumber.runtime.io.ZipResourceIterator.<init>(ZipResourceIterator.java:22)
at cucumber.runtime.io.ZipResourceIteratorFactory.createIterator(ZipResourceIteratorFactory.java:24)
at cucumber.runtime.io.ZipThenFileResourceIteratorFactory.createIterator(ZipThenFileResourceIteratorFactory.java:22)
at cucumber.runtime.io.DelegatingResourceIteratorFactory.createIterator(DelegatingResourceIteratorFactory.java:49)
at cucumber.runtime.io.ClasspathResourceIterable.iterator(ClasspathResourceIterable.java:35)
at cucumber.runtime.io.ResourceLoaderClassFinder.getDescendants(ResourceLoaderClassFinder.java:25)
at cucumber.runtime.Reflections.instantiateSubclasses(Reflections.java:34)
at cucumber.runtime.BackendModuleBackendSupplier.loadBackends(BackendModuleBackendSupplier.java:52)
at cucumber.runtime.BackendModuleBackendSupplier.get(BackendModuleBackendSupplier.java:39)
at cucumber.runner.ThreadLocalRunnerSupplier.createRunner(ThreadLocalRunnerSupplier.java:42)
at cucumber.runner.ThreadLocalRunnerSupplier.access$000(ThreadLocalRunnerSupplier.java:13)
at cucumber.runner.ThreadLocalRunnerSupplier$1.initialValue(ThreadLocalRunnerSupplier.java:22)
at cucumber.runner.ThreadLocalRunnerSupplier$1.initialValue(ThreadLocalRunnerSupplier.java:19)
at java.base/java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:195)
at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:172)
at cucumber.runner.ThreadLocalRunnerSupplier.get(ThreadLocalRunnerSupplier.java:38)
at cucumber.api.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:146)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
This is quite puzzling because:
when running mvn verify locally, it works
when using agent any in the Dockerfile, instead of the maven Docker image, it works
when using maven-surefire-plugin version 2.22.2 instead of 3.0.0-M3, it works
I logged in as Jenkins and looked in the workspace:
$ sudo su - jenkins
$ ls /var/lib/jenkins/workspace/question-mark-dir_master
'?' Jenkinsfile LICENSE pom.xml README.md src target
There's a question mark (?) directory in there, and it contains the .m2 directory, with all the artifacts needed by the project.
I suspect this could be the cause of the exception, because then there are question marks in the classpath, and question marks are the ones who introduce the query component of URIs.
For the moment I can downgrade to maven-surefire-plugin version 2.22.2 as a workaround.
But what is the actual problem here?
Could it be a bug in the new maven-surefire-plugin? Judging by the stack trace I wouldn't bet on that.
Is it a problem in Jenkins or in the Maven docker image (which one?), that causes a directory to have such an unfortunate name?
Am I doing something wrong?
If you want to try this out, I have a MVCE.
In my local Jenkins installation, I created a new project of type "Multibranch Pipeline" and in "Branch Sources" I added as the git project repository my local git project. No other changes.
By combining TYY's comment to the question and the documentation of the maven docker image, I was able to configure the Jenkinsfile in a way that avoids creating a question mark directory.
We need to inform Maven of the user's home directory, and map that to a directory outside the container.
pipeline {
agent {
docker {
image 'maven:3.6.1'
args '-v /var/lib/jenkins:/usr/src/mymaven -w /usr/src/mymaven'
}
}
stages {
stage('build') {
steps {
sh 'MAVEN_OPTS="-Duser.home=/usr/src/mymaven" mvn clean verify -Dmaven.test.failure.ignore=true'
}
// ...
}
}
}
-v /var/lib/jenkins:/usr/src/mymaven maps the /var/lib/jenkins directory on the host to the /usr/src/mymaven directory inside the container
-w /usr/src/mymaven sets the working directory inside the container
MAVEN_OPTS="-Duser.home=/usr/src/mymaven" sets the user.home Java property for Maven
Proof that it works, on a branch of the MCVE.

Jenkins + Maven release plugin: release:branch + Git - Unable to commit files

I'm trying to make a release using the Maven release plugin, but I get an error
[ERROR] Command output:
[ERROR] GitLab: The project you were looking for could not be found.
[ERROR] fatal: The remote end hung up unexpectedly
In my pom.xml such settings:
<scm>
<developerConnection>scm:git:ssh://git#git.domain.ua/myproject/mylib.git</developerConnection>
<url>https://git.domain.ua/myproject/mylib</url>
<tag>HEAD</tag>
</scm>
My Jenkins job settings:
Repository URL: ssh://git#git.domain.ua/myproject/mylib.git
Credentials: my ssh-key also imported in Git repo as deploy key
Branch Specifier (blank for 'any'): */development
Maven Release goals and options: -Dresume=false release:branch javadoc:javadoc
DryRun goals and options: -Dresume=false -DdryRun=true release:prepare
As you can see from the logs, Git-plugin successfully clones a branch, but Maven cannot make a commit for some reason:
Cloning the remote Git repository
Cloning repository ssh://git#git.domain.ua/myproject/mylib.git
> git init /opt/jenkins/workspace/MYJOB # timeout=10
Fetching upstream changes from ssh://git#git.domain.ua/myproject/mylib.git
> git --version # timeout=10
using GIT_SSH to set credentials JENKINS(jenkins user with jenkins#USER)
git checkout development
Switched to a new branch 'development'
Branch development set up to track remote branch development from origin.
Parsing POMs
Established TCP socket on 44626
[INFO] Executing: /bin/sh -c cd /opt/jenkins/workspace/MYJOB && git push ssh://git#git.domain.ua/myproject/mylib.git refs/heads/development:refs/heads/development
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.1:branch (default-cli) on project MYPROJECT: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[JENKINS] Archiving /opt/jenkins/workspace/MYPROJECT/pom.xml to *****-SNAPSHOT.pom
[ERROR] Command output:
[ERROR] GitLab: The project you were looking for could not be found.
[ERROR] fatal: The remote end hung up unexpectedly
I guess Maven doesn't know anything about the key that uses the Git-plugin, but I can't figure out how to configure the Maven Release Plugin to use the same key?
Try doing this in your Jenkins job configuration:
Update the Branch Specifier (blank for 'any') to the syntax refs/heads/development.
Under SCM > Git > Additional Behaviours, select Checkout to specific local branch and then enter ** as the branch name.
Under Build Environment, select SSH Agent > Specific credentials, and then specify the same SSH key that you used for cloning the repository.
The error is in the clone via https protocol and the push via ssh protocol. You must define which protocol to use for the transfer. For comparison, use the result of the command: git remote show origin, with the scm parameter set in the pom.xml file.
Example:
<scm>
<connection>scm:git:git#github.com:user/repo-name.git</connection>
<developerConnection>scm:git:git#github.com:user/repo-name.git</developerConnection>
<url>git#github.com:user/repo-name.git</url>
<tag>HEAD</tag>
</scm>
try to remove the ssh:// and just use : scm:git:git#git.domain.ua/myproject/mylib.git
replace the url tag by a connection tag with the value : scm:git:git#git.domain.ua/myproject/mylib.git
In the end it's gonna be something like this :
scm:git:git#git.domain.ua/myproject/mylib.git
scm:git:git#git.domain.ua/myproject/mylib.git
Also the plugin is searching for a project "MYPROJECT" uppercase , or i guess in gitlab youhave "myproject", you can check and change all "MYPROJECT " reference to "myproject" and then try again.
Best regards,

Run simple Rest Java hello world program

I am trying to run a simple Rest HelloWorld program as described in https://spring.io/guides/gs/rest-service/. I have downloaded Spring and Maven and added the MAVEN_HOME and M3_HOME environment variables. I have run the mvn -v command successfully in Windows command prompt. I browsed to the initial directory where the pom.xml is located. Then I run mvn spring-root:run but I get the following error:
No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\user.m2\repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
I have already searched for this error message on the internet but the suggestions are not solving my problem. Am I forgetting something related to any installation? Any idea or suggestion is highly appreciated.
Oh, I realized now I had same issue in the past...
To address
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) on project gs-rest-service: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.9.RELEASE:repackage failed: Unable to find main class -> [Help 1]
You have to create Application class (something annotated with #SpringBootApplication)
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
or skip tests mvn clean install -DskipTests.
For mvnw spring-boot:run I'm getting ConnectException right now...
Downloading https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
Exception in thread "main" java.net.ConnectException: Connection refused: connect
but it is very likely issue on my side...

Maven central repo: Return code is: 503 , ReasonPhrase:backend read error

In the continuous integration system (travis-ci.org) on our project, I'm starting to get error:
[ERROR] Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.6-SNAPSHOT:start (start-container) on project p6spy-it: Failed resolving artifact: Could not transfer artifact org.jboss.jbossas:jboss-as-distribution:zip:6.1.0.Final from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/jboss/jbossas/jboss-as-distribution/6.1.0.Final/jboss-as-distribution-6.1.0.Final.zip. Return code is: 503 , ReasonPhrase:backend read error.
[ERROR] org.jboss.jbossas:jboss-as-distribution:zip:6.1.0.Final
[ERROR]
[ERROR] from the specified remote repositories:
[ERROR] codehaus-snapshots (http://nexus.codehaus.org/snapshots/, releases=false, snapshots=true),
[ERROR] cloudbees-release (http://repository-p6spy.forge.cloudbees.com/release, releases=true, snapshots=false),
[ERROR] sonatype (https://oss.sonatype.org/content/repositories/releases/, releases=true, snapshots=false),
[ERROR] sonatype-snapshots (https://oss.sonatype.org/content/repositories/snapshots/, releases=false, snapshots=true),
[ERROR] sonatype-apache (https://repository.apache.org/releases/, releases=true, snapshots=false),
[ERROR] apache-snapshots (https://repository.apache.org/snapshots/, releases=false, snapshots=true),
[ERROR] sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots, releases=false, snapshots=true),
[ERROR] central (http://repo.maven.apache.org/maven2, releases=true, snapshots=false)
Full build output can be seen on: https://api.travis-ci.org/jobs/15104889/log.txt?deansi=true
If I access the problematic url via browser: http://repo.maven.apache.org/maven2/org/jboss/jbossas/jboss-as-distribution/6.1.0.Final/jboss-as-distribution-6.1.0.Final.zip
I get the same error as well:
Error 503 backend read error
However one day back all was fine.
What should I do with that/How to fix that? Just wait for them to refresh some internal caches?
Try:
(1) Download jboss-as-distribution-6.1.0.Final.pom and jboss-as-distribution-6.1.0.Final.zip from the Maven Central Repository.
(2) Install the zip file into your local Maven repository manually.
mvn install:install-file \
-Dfile=jboss-as-distribution-6.1.0.Final.zip \
-DpomFile=jboss-as-distribution-6.1.0.Final.pom
It seems "Return code is: 503 , ReasonPhrase:backend read error." error occurs when the target zip file is big. I encountered the same error when downloading appengine-java-sdk-1.9.0.zip.
If downloading remote artifacts is mission critical for your build, have you considered running your own instance of Nexus?
http://www.sonatype.org/nexus/
This will protect you against transient networking and server errors, by acting as a local cache inside your network. It'll also improve the performance of your build processes.
A second lessor option is to use the alternate URLs provided by the Search service:
jboss page on Maven Central
http://search.maven.org/remotecontent?filepath=org/jboss/jbossas/jboss-as-distribution/6.1.0.Final/jboss-as-distribution-6.1.0.Final.zip
Maybe, just maybe you'll get redirected to a webserver that is either hosted on S3 or in a more available cluster.
Finally if you're interested in what Sonatype are doing to scale up Maven Central, their blogs have postd some interesting updates:
http://blog.sonatype.com/people/tag/central/
http://blog.sonatype.com/people/2012/07/we-just-kicked-central-performance-and-availability-up-a-notch-with-edgecast/
http://blog.sonatype.com/people/2008/12/central-maven-repository-taffic-using-s3/

Categories