Multiple maven install commands in Linux - maven

I am running an azure pipeline for a maven project which has windows commands for maven installation, by calling multiple methods i.e.,
call mvn ... clean install
call mvn ... clean package (authentication)
call mvn ... clean package (restapi)
The build environment is linux Hence I am converting all the commands in the batch file to sh commands. For maven installation I initially added a maven installation task by mentioning the path and commands in the task. This failed.
So I am currently changing the windows commands to sh commands. The other commands except for maven installation have been converted using a batch to sh commands reference article.
Could some one guide me to convert the above mentioned installation commands to sh commands?

If the Apache Maven has been installed and added to the system path on the agent machine where your pipeline runs, you can just try directly calling the mvn command in the bash script, like you call it on Windows.
For example:
mvn ... clean install
mvn ... clean package (authentication)
mvn ... clean package (restapi)
Before you execute the bash script in the pipeline, you firstly should try and debug it on your local machine to make sure it can work as expected on the local machine. Then move the bash script to the pipeline on Azure DevOps.
If the bash script runs failed in the pipeline, for us to investigate this issue further, please share us with the complete debug logs of the failed pipeline run. To get the debug logs, you need to set the pipeline variable System.Debug to true, then trigger the pipeline.

Related

mvn not found when executed via Jenkins shell script

I have set M2_HOME under Manage Jenkins -> configure system -> Environment variables and also under Manage Jenkins -> global tool configuration.
When I use maven project, it is working fine.
But when I use freestyle project and execute shell, it throws an error "mvn not found".
But when I give echo $M2_HOME in Execute shell, it shows the correct path.
Also, when I tried $M2_HOME/mvn compile, it worked fine.
I could not figure out the exact issue.
Error
[Compile]
$ /bin/sh -xe /tmp/jenkins1984982949384007169.sh
+ mvn --version
/tmp/jenkins1984982949384007169.sh: 2: /tmp/jenkins1984982949384007169.sh: mvn: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
The shell which executes the mvn command doesn't know that the path for the script in in the M2_HOME variable. The shell will only look at commands listed in the $PATH variable.
So there are two solutions:
Always use "$M2_HOME/mvn" to execute Maven (or rather "$M2_HOME/bin/mvn" since M2_HOME should point to the folder which contains bin and lib and not to the script itself).
Change the path at the top of the script to make the shell search in the right place:
export PATH="$M2_HOME/bin:$PATH"
That probably only makes sense when you run Maven more than once in the script.

nohup: failed to run command `sh`: No such file or directory

I have the following pipeline script in Jenkins:
node {
withMaven(globalMavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml', jdk: 'JDK 1.8.0u92', maven: 'apache-maven-3.2.2', mavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml') {
sh '/my/path/apache-maven-3.2.2/bin/mvn clean install'
}
}
For this, I am getting:
nohup: failed to run command `sh`: No such file or directory
ERROR: script returned exit code -2
Why is this?
I am sure that the path to my Maven installation is correct. When I run a job without the pipeline, Maven builds with no errors and I can see that it uses the same path.
This might be the result of modifying PATH.
Check your script and Global Properties and remove modifications to PATH. It is now recommended to use PATH+extra instead. It would still be picked up, but without breaking actual PATH.
Related issue on Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-41339
In the end, I used shell instead of sh and it worked. No idea why, they don't have a proper API.
I would suggest to use it like this:
withMaven(
maven: 'M3',
mavenSettingsConfig: 'maven-settings-for-the-task',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
}
Apart from that I would not use absolute paths to global settings.xml nor to user settings.xml. I would prefer using usage of "Config File Provider Plugin" which has the advantage to have the settings.xml on Master and available on all nodes.
See also the documentation: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Maven+Plugin
This error comes when you are trying to run script copied from windows machine to unix machine.
YOU need to change the format to unix using : dos2unix <scriptname.sh> and the run your script in unix ./<scriptname.sh>

Can't execute "mvn clean package" task in GO CD

I have setup a "Hello World" pipeline with one task mvn clean package in Go CD. I have registered an agent with Java and Maven up and running.
When I trigger the pipeline, the job fails:
12:05:08.655 [go] Start to execute task: <exec command="mvn" > <arg>clean package</arg> </exec>.
12:05:08.660 Error happened while attempting to execute 'mvn clean package'. Please make sure [mvn] can be executed on this agent.
If I execute mvn clean package in my agent, everything works. What is happening? Is there a place where I can see more specific logs?
Instead of running the following:
Command:
mvn clean package
try to use
Command:
/bin/bash
Arguments:
-c
mvn clean package
I struggled with the same issue and figured out a solution which worked for me. Maybe, it will help you.
In the command prompt of your agent enter echo $PATH
This will show all your path variables. Copy all of them.
Now, in the GUI of your server, choose the configuration of your
pipeline and add a PATH variable with the copied variables.
You can use:
Command:
mvn
Arguments:
clean
package

How to execute remote bat file using build step in TeamCity?

I have TeamCity installed on centos. I have only one Linux BuildAgent for now. My build configuration execute a maven script and using ant upload WAR artifact to Windows Server on FTP. After this step i have to execute BAT file on remote Windows Server. I read that i can do this using psexec/RemCom, but i can't understand how i can do this in TeamCity? Build Step or different Build Configuration should contain steps to execute psexec/RemCom or i can insert Build Step into existed Build Configuration?
Single build configuration could potentially consists of multiple build steps. Think of them as a actions you would like to do. So I suppose that right now you have a maven step in your build configuration. I would suggest you to add Command Line step, where you can do what ever you want -- it's like bash/batch script. You can put script contents directly to the build step, or you can write script and execute it.
But as you mentioned that you have CentOS, it could be you have to do extra configuration on the build agent for PsExec to be available.

Running a Grunt task before MVN in a Build pipeline?

This question is very specific to Bluemix DevOps.
I have a Java backend application that has a sizeable JavaScript front-end. So I created a GRUNT task to do the needed: uglify, minify, CDNify etc. My current setup is to have the Bluemix build just running mvn -B package and the Grunt task beforehand as a script on my local machine:
#!/bin/bash
grunt build
git add --all
git commit
git push origin master
But that precludes any edit using the online editor. So I'd like to have both task to run by the pipeline. I see 3 options:
Run both tasks in one build block triggered by git push as separate tasks
Run them in one build script triggered by git push
Run 2 pipeline steps, the first triggered by git push, the second by the completion of the first
something else
I haven't tried it yet (shame on me), just wanted to ask if someone did that before (If yes - cool, if no I will post my findings later on)
Solved it. This is what I tried:
modify the script in build and prefix with npm install npm or mvn (depends on what I selected) wasn't found)
add 2 jobs to one build stage, one grunt one maven (the deploy task would not find the war file)
Use 2 pipeline stages (see picture below) : Horray --- that worked.
None of the build steps required setting a directory, which is a little trap, since mvn sets target as default directory, so remove this. The script for Bower/Grunt is this:
#!/bin/bash
npm install
grunt build
the script for the maven task:
#!/bin/bash
mvn -B package
works like a charm (just be careful not to add npm modules you don't actually need, it slows the build quite a bit)

Resources