Location of Maven in Jenkins - java

In our organization, we have a machine that is configured to run our jenkins jobs.
There are two versions here.
In this version, a maven template is used for new item, and the repository name and settings.xml and pom.xml files locations are specified. And after this build step, I have mvn -version which gives some version and the location of the maven version. This works fine.
In this version, a pipeline template is used for new item. When I run the mvn -version, it doesn't have the path set for maven so it says mvn command not found. So I take the path from the previous version, and plug it here for maven in the path. Now I run the path/to/maven/mvn -version command, it says JAVA_HOME is not defined. When I execute the export JAVA_HOME=/path/to/java the JAVA_HOME path is not set(may be the reason might be I don't have the rights to set environment variables).
So how do I run the maven clean install using the pipeline version (I dont have permission to install plugins also)

Related

How to Run Eclipse+Maven+JUnit tests in Command Line

When I run mvn clean test, it gives me the following error:
'mvn' is not recognized as an internal or external command, operable program or batch file."
This is the link of my project.
I can run the tests by right-clicking on the Runner.java => Run as => JUnit Test. I can also run the tests from the JUnit 4 Test Explorer Tab.
I have read some articles where it's suggesting to set the PATH for Maven but all of them are about the case where you are installing Maven separately. I am not sure what PATH do I need to set when Maven is coming with Eclipse.
I did not install Maven separately, it came with my Eclipse version.
My Eclipse Version :
Eclipse IDE for Java Developers
Version: 2019-03 (4.11.0)
Build id: 20190314-1200
Some IDEs (IntelliJ for example) have their own version of maven for internal purpose. But it doesn't mean that's able to be used by the command line. If you want to compile your project using mvn clean install/package/deploy/test/etc you must install maven in your machine and set de VARs in the OS.
I guess Eclipse has the same behavior that Intellij, so please also install maven in your machine
This error means that Windows cannot find an executable program called "mvn". This means it is not on your path/current directory.
When you right click and run your junit tests, it invokes it using the JVM, using the java command. I suspect if you type "java -v" into CMD, it will not give you the same error.
Since Windows does not know where to look for maven, you must tell it implicitly, by setting your path to the directory containing the mvn executable. You can lookup "eclipse bundled maven default location" to find this, although it might be worth downloading it separately from the Apache website, and manually specifying the path.

Jenkins config JDK for project and maven build always get error

Jenkins version: Jenkins ver. 2.107.2
How to configure the Jenkins project to use the custom JDK with Maven?
I have added JDK in the Global Tool Configuration and Maven with automatically install. I add two version of JDK.
Then I create a maven project by specifying one of the JDK and one of Maven. But I always get the error of mvn cannot find the java as JDKfollows:
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven- 3.5.3/bin/mvn package
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven-3.5.3/bin/mvn: exec: line 199:
/var/jenkins_home/tools/hudson.model.JDK/jdk8-172/bin/java: not found
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
Then I try a lot of methods, only the system OpenJDK is work (chose JDK with System on the Jenkins project)
I also try
config JAVA_HOME to the installed JDK
config Tool Path
I also check the mvn and java in the Jenkins master(I use Docker to run the Jenkins), the JAVA_HOME is the OpenJDK and the mvn is not set.

Cannot run Maven on Jenkins with Ubuntu

I am struggling to get a simple maven project running on Jenkins, which runs on my new Ubuntu server (16.04.)
I have installed Jenkins onto an Ubuntu, installed Maven and JDK.
I can connect to bitbucket and access my remote repository and get hold of my code. However, when I try to build I get the message
cannot run program "mvn" (in directory "/var/snap/jenkins/workspace/project") no such file or directory
I have added $JAVA_HOME and $MAVEN_HOME to my /etc/environment as variables and also into my path.
I have looked at the Global Tool Configuration, but this seems to be no help.
When I set the Maven installer to point at Maven home I get a message
/usr/share/maven is not a directory on the jenkins master
When I try to get Jenkins to install maven for me I get
Failed to install <truncated>apache-maven-3.5.3.bin.zip to /var/snap/jenkins/6/tools
...
Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must not be empty
I think I have hit a dead end. What could I be missing, or how can I get past these obstacles ?
Many Thanks
Go to Manage Jenkins -> Global Tool Configuration. I always elect to have Jenkins install and manage Maven, vs install and bring my own.
It has taken a long time to get to the bottom of this.
By default the
sudo apt-get install jenkins
seems to install a Snap version of Jenkins.
This snap version holds data in different locations (which is why I struggled to find the initialAdminpassword
Additionally, the snap setup only has a JRE, which cannot be overridden to a JDK, and therefore does not seem to work properly with Maven.
Eventually, I completely uninstalled Jenkins, and then followed the instructions on the official Jenkins site:
https://pkg.jenkins.io/debian-stable/
This overrides the base apt-get packages, and points to the jenkins.io packages.
From here on in, it all behaved as per the documentation.

What is the purpose of mvnw and mvnw.cmd files?

When I created a Spring Boot application I could see mvnw and mvnw.cmd files in the root of the project. What is the purpose of these two files?
These files are from Maven wrapper. It works similarly to the Gradle wrapper.
This allows you to run the Maven project without having Maven installed and present on the path. It downloads the correct Maven version if it's not found (as far as I know by default in your user home directory).
The mvnw file is for Linux (bash) and the mvnw.cmd is for the Windows environment.
To create or update all necessary Maven Wrapper files execute the following command:
mvn -N io.takari:maven:wrapper
To use a different version of maven you can specify the version as follows:
mvn -N io.takari:maven:wrapper -Dmaven=3.3.3
Both commands require maven on PATH (add the path to maven bin to Path on System Variables) if you already have mvnw in your project you can use ./mvnw instead of mvn in the commands.
Command mvnw uses Maven that is by default downloaded to ~/.m2/wrapper on the first use.
URL with Maven is specified in each project at .mvn/wrapper/maven-wrapper.properties:
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
To update or change Maven version invoke the following (remember about --non-recursive for multi-module projects):
./mvnw io.takari:maven:wrapper -Dmaven=3.3.9
or just modify .mvn/wrapper/maven-wrapper.properties manually.
To generate wrapper from scratch using Maven (you need to have it already in PATH run:
mvn io.takari:maven:wrapper -Dmaven=3.3.9
The Maven Wrapper is an excellent choice for projects that need a specific version of Maven (or for users that don't want to install Maven at all). Instead of installing many versions of it in the operating system, we can just use the project-specific wrapper script.
mvnw: it's an executable Unix shell script used in place of a fully installed Maven
mvnw.cmd: it's for Windows environment
Use Cases
The wrapper should work with different operating systems such as:
Linux
OSX
Windows
Solaris
After that, we can run our goals like this for the Unix system:
./mvnw clean install
And the following command for Batch:
./mvnw.cmd clean install
If we don't have the specified Maven in the wrapper properties, it'll be downloaded and installed in the folder $USER_HOME/.m2/wrapper/dists of the system.
Maven Wrapper plugin
Maven Wrapper plugin to make auto installation in a simple Spring Boot project.
First, we need to go in the main folder of the project and run this command:
mvn -N io.takari:maven:wrapper
We can also specify the version of Maven:
mvn -N io.takari:maven:wrapper -Dmaven=3.5.2
The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules.
Source 1 (further reading): https://www.baeldung.com/maven-wrapper
short answer: to run Maven and Gradle in the terminal without following manual installation processes.
Gradle example:
./gradlew clean build
./gradlew bootRun
Maven example:
./mvnw clean install
./mvnw spring-boot:run
"The recommended way to execute any Gradle build is with the help of the Gradle Wrapper (in short just “Wrapper”). The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary. As a result, developers can get up and running with a Gradle project quickly without having to follow manual installation processes saving your company time and money."
Gradle would also add some specific files corresponding to the Maven files Gradlew and Gradle.bat
In the windows OS, mvnw clean install is used for the maven clean and install activity, and mvnw spring-boot:run is used to run the Spring boot application from Command Prompt.
For Eaxmple:
C:\SamplesSpringBoot\demo>mvnw clean install
C:\SamplesSpringBoot\demo>mvnw spring-boot:run
By far the best option nowadays would be using a maven container as a builder tool. A mvn.sh script like this would be enough:
#!/bin/bash
docker run --rm -ti \
-v $(pwd):/opt/app \
-w /opt/app \
-e TERM=xterm \
-v $HOME/.m2:/root/.m2 \
maven mvn "$#"

Unable to configure Maven in Jenkins

Maven version 3.3.3 (Maven is configured in my system. I'm able to get version number by mvn -version.)
Following is my Maven project in Jenkins:
Which basically asks me to configure Maven.
Below are the only fields available when I select Configure System. Though I gave the path of Maven, the error is not removed and unable to build.
Can someone help on this.
Please check "Jenkins => Configuration => Global Tool Configuration". There you can define your maven-installation not only the the environment-variable MAVEN_HOME.
Afterwards you should be able to select your maven-installation in your project-build.
ln -s /opt/apache-maven-3.5.4/bin/mvn /usr/bin/mvn
run echo $PATH in pipeline script and see paths available to Jenkins user. Once linking with maven bin path, maven started working. To check, run mvn -v in the pipeline.
First you should configure a JDK and NOT an JRE. Furthermore the rootPOM location is relative to the Jenkins Job Workspace which means usually only pom.xml nothing more...

Categories

Resources