Access Maven build properties in Jenkins post-build script to retrieve deployed artifact - maven

I've got a Maven project that Jenkins builds and deploys to a remote repository. I then need to copy the deployed .war to an external location. I've been trying to do this with a post-build shell script but I don't see any way to get the build information from maven (for example, the URL of the deployed artifact). Is there a way to get it, or a way to do this that's more integrated into maven? I can calculate the deployment path using Jenkins build parameters but it seems like a hack.
Thanks,
Steve

After a maven build you should always find the build artifact at
target/<artifactId>-<version>.<packaging>
You can access this path within the maven pom.xml by using the maven properties (see pom reference)
${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}
To copy the artifact to another location after the build you can use several approaches described e.g. in this thread.

Related

Unable to deploy springboot jar in bamboo after using maven-release

I am using a Bamboo CICD pipeline to build and deploy spring boot jar using maven release. Build is working fine and a spring boot jar is created. Since maven release updates the jar version after every run, I am unable to use this jar in the next stages of bamboo pipeline.
I have gone through bamboo documentation and discussion forums in Atlassian and Stackoverflow but unable to get the required solution. I tried to get the artifact in the next stages using "Artifact Download" task but unable to use it since jar version always varies.
I tried to fetch the shared artifact file using regular expression such as target/*.war in the deploy step (Command task - Argument field) but guess bamboo is not able to execute the regex. It is giving the following error message.
Incorrect Usage: The specified path 'target/*.war' does not exist.
If I give the correct name such as target/appname-0.0.1-snapshot.war bamboo is able to find the war file and deploying it successfully.
Is there any way that the exact war or jar name can be obtained in further stages without the need of manual update after every run? Any help on this would be greatly appreciated.
Regular expression is not working in the command task but I was able to resolve this issue by using the bamboo script task. Regular expression is working fine and target/*.war has been resolved correctly.
You can also try Maven POM Value Extractor plugin. This plugin provides a build task that extracts values from Maven POMs and sets build variables using those values. This allows you to keep you Bamboo variables in sync with your Maven POM.
You can automatically extract your artifact's GAV (GroupId, Artifact, Version).

How to download maven dependencies from Jenkins without a binary repository

Are there any plugins or ways to download the dependencies for a maven project from Jenkins? I am using Jenkins for a multi-module desktop application. Although I know I could just archive all dependencies, I don't see why there isn't the ability to download dependencies using maven which installed on the same machine as Jenkins. Preferably one would specify the location of a pom and then have the ability with one click to download all the dependencies for that pom. Can you do this? I do not need or want an entire binary repository for this feature.
Edit: I will try and rephrase this as I don't think people are understanding.
In Jenkins one has the ability to archive artifacts at the end of a build. Also in jenkins you have integration with maven. When building a jar in maven you have arguablly 2 options:
You can either use the assembly plugin which zips all .class files
together with those produced from your source code resulting in 1 jar
You can create a jar just source code which references all
dependency jars which are located in a separate folder.
In Jenkins one also has the ability to download the latest artifact. Now if I am using Option 2, I can either archieve just the jar which my sources produced, which I would say is more desirable for space and is the whole purpose of the archive functionality, or you can also archive the libraries too.
Here is the PROBLEM!! If I don't archive the libraries then I cannot easily run this jar, as it is a desktop application and its dependencies cannot be obtained in the same mannor as clicking on a link from jenkins. So lets say my question is what is the easiest way to obtain them? Extra info: assume jenkins is running as a server and you can't use artifactory or another server application, that seems to me to be massive over kill.
Use the maven plugin and create a maven job for your project. Jenkins will then use the maven command you provide in the job configuration to build the project. This means maven will download the projects dependencies and store them on the machine jenkins is running. Normally this would be <JENKINS_HOME>/.m2/repository. This way you get a local repository that only contains the dependencies of the projects you created maven jobs for.

Jenkins - How to pass values from pom.xml to downstream job (free style)

I've set up 2 free style jobs, build-app and deploy-app. build-app poll the scm and builds the app, which is Maven based, and install the artifact in a web server (internal repository server), then it calls deploy-app. I would like to pass the version of the pom file () to the downstream job so it can download the correct artifact and install it on the machine. I found some answers suggesting put the version string in a properties file and use InjectEnv plugin, but I prefer read it from the pom itself. Any ideas?
Thanks!
When you build inside Maven, you have access to the pom file version as ${program.version} and can do anything you wish with it.
The downstream freestyle job can also run Maven using the same pom but a different target. The version should be the same if you take care to keep it from changing in the interim. (This suggests a procedure that should always be followed.)
So, for example, that Maven target can run a Groovy script or an Ant script that will pick up the correct file from the repo and deploy it.

How to use ANT in Cloudbees

I'm trying to move an existing ANT build script (build.xml) into Cloudbees for CI using Jenkins. I setup the project repository using GitHub. The build pulls the repository into the Cloudbees Workspace successfully, but then fails with this message.
Parsing POMs
ERROR: No such file /scratch/jenkins/workspace/project/pom.xml
Perhaps you need to specify the correct POM file path in the project configuration?
I'm not too familiar with this, but from what I can tell (thanks Google) it's because Cloudbees uses Maven instead of ANT. Is there a way that I can change Jenkins to run the ANT build script instead of using Maven? Or a simple way to execute my ANT scripts from Maven? Any help here would be appreciated!
Thanks
You do not need a Maven POM file if your project is Ant-based.
It sounds like you created a Maven job in Jenkins. Delete it and create a free-style job instead, then (as #thekbb says) click Add build step and select Invoke Ant and configure as needed.
ant support is provided by teh ant plugin, I think you get this automatically when installing jenkins. In your jenkins job, add a build step of type 'Invoke Ant' and provide the target.

Configure Maven or Nexus to link trunk artifact at static URL

My current Jenkins deployment job retrieves war file generated from maven build process from Nexus repository. The deployment is done this way since I can not use hot deployment for my environments. Currently I used parameterized build with Jenkins so I can manually enter the version number for my artifact. Is there a way to configure Maven or Nexus so the artifact generate from the latest trunk build can be accessed from an static URL? For example:
http://mynexus:8081/nexus/content/repository/snapshots/com/somepackage/my-app/trunk/my-app-trunk.war
I don't know a way to do this in Nexus. But you can access the latest successful build from Jenkins, with a URL like this: http://localhost:8080/jenkins/job/jobname/lastSuccessfulBuild/my-app-trunk.war
You have to enable artifact archiving for your war file, then you can access it.
Same issue here, we discovered about :
https://wiki.jenkins-ci.org/display/JENKINS/Maven+Deployment+Linker
Which does the job.
Hope that helps.

Resources