Creating checksums for generated files in maven when executing mvn package - maven

I have a maven project to create a zip file which can be used to upgrade the applications we distribute. When mvn package is executed this will gather required dependencies and bundle with some PowerShell scripts which does the upgrade. I want to create a checksum for the generated zip file.
When i searched over internet i got two solutions one is to use -DcreateChecksum=true option for the mvn install plugin. This zip file is big and we don't want to copy this to maven repository hence we don't execute this goal. Another option is using checksum-maven-plugin, but this will create checksum for artifacts which i don't want. It also has a command line option mvn checksum:file -Dfile=some-file.zip but this has to be executed manually. I want to add this to my pom so we don't have to manually create checksum. Is there any clean way other than creating a script to create checksum and calling it.

Related

Force maven to always fetch artifacts from target folder

Circumstances
It would be nice to split long running build jobs, espescially in multi module projects.
Compile everything mvn clean install or mvn clean package
Execute unit tests on each module mvn surefire:test
Execute integrations tests ant and stuff
Publish artifacts to a remote repository mvn my.own.tools:publish-plugin:publish
Execute post build steps (Tagging, etc) build server stuff
In some build environments, like Atlassian Bamboo, each step will be likely executed on another build agent as the previous one. They can even have their own local repository each. However, it is possible to copy all the files of the working directory to that of a subsequent stage.
Observation
Maven uses the contents of the target folders before it makes a lookup into the local repository or a remote repository. This is true when the contents of the target directories are created by a previous phase during a specific run.
Example: mvn clean install surefire:test
If a test has dependencies, maven will look at first in the target directories which have been created during the compile phase.
If the command is split into two, it seems that maven does not recognize the target folders at all.
Example: mvn clean install; mvn surefire:test
Now maven loads all the dependencies from the local repository or, if they are not there, from a remote repository.
Problem(s)
During step 2 maven ignores the contents inside the target foldera, which have been created during step 1.
Step 2 runs on a different build server, than step 1. However, the whole file structure (including the target folders) from step 1 have been copied into the working directory from step 2. The artifacts which the tests depend on, are not taken out of those directories. Since each build server has it's own local repository, they are not found and on the remote repository they have never been uploaded.
Any idea of making our whole beast more modular and split up the project is unfortunately no possiblity
Question
How can maven be forced to load dependencies out of existing target folders of a multi module project under any circumstances? I understand that maven is not aware of those folders in step 2 since they are not created during that maven run. But how can I force maven to look if those folders exists?
More elaborted problem ;-)
Our "Publish-Plugin" in step 4 seems to work mostly fine and uploads the artifacts out of the target folders. The project iformation is gathered inside the Mojo by usual maven properties. But there are some zip files created during step 1 by the org.apache.maven.plugins:maven-assembly-plugin. Those zip files are not found, although they are residing inside the target folder. Now maven tries to download them, what is funny, because it does that to have them in hand for a upload.

Maven install assembly exclusion

In my Maven project, I am using the maven-assembly-plugin bound to the package phase to create a distribution archive (zip) of the project. When I call mvn clean install to build & install the project, this archive gets installed into my local repository (.m2) as well. How can I exclude it from the installation? Or more generally, how can I exclude certain files/maven products from installation? I tried to relocate the assembly output out of the target directory but that wasn't the solution.
You can set the attach property of the maven-assembly-plugin to false, see the docs.
That way the file is generated but not attached to the project, excluding it from operations like install and deploy.
That being said, I don't recommend this as it can lead to unexpected behavior down the line.

Copy file after successful build using maven

I'm using mvn install to build the .hpi to my plugin for Jenkins. This puts a .hpi in the target folder.
What I would like to do is if the build is successful, copy the hpi-file to a specific folder. Is it possible to do both these task with one Maven command using only the terminal/command prompt?
edit
My POM can be seen here.
https://github.com/MarkusDNC/plot-plugin/blob/master/pom.xml
There is now way to copy file through MVN CMD.
After that being said - You have 2 options:
1.) using a simple shell/batch step to copy.
2.) You need to Add another build step for maven but you will also need a new pom.xml file and use maven-antrun-plugin

how to I pack external tools in the package during maven build

I have an existing mvn project which build into a zip file at the end.
now I need to add an external tool to it, the tool is already a single executable so I just need to include it in the final package.
what is the right way to do this?
Thanks,
There are three steps to follow:
Upload your artifact/tool to a maven repository, e.g. on your local maven repository server (Archiva/Artifactory/Nexus etc.)
Use the Maven Dependency Plugin to download your artifact/tool to your project target directory where you assemble your zip file
Use the Maven Assembly Plugin to assemble your zip file

What is the most common way to unpack a jar file after jenkins maven build?

I am new to maven and Jenkins so I do not know what is the most common way to extract the JAR file build by maven in the same Jenkins job.
Running mvn install in a Jenkins job outputs the file /home/user/.jenkins/workspace/$JOB_NAME/project/target/package-2.0.0.jar.
I want to extract it to some directory like /opt/project and call /opt/project/script.sh.
I thought of a post-build shell script calling jar -xvf <path>/package-2.0.0.jar but how to get the version number (2.0.0) then? Maybe there is a maven goal to do this?
define that artifact as a dependency in some other module (the module that will run the shell script) and use the dependency plugin to unpack it
that would mean you'd have (at least) 2 modules in your maven project - one that produces the jar, and the other that does something with the artifact produced by the 1st.
if that doesnt fit your need you could bind the unpack after the install phase (the artifact makes it into the local repository at the install phase, and the dependency plugin only deals with artifacts from the local repository) and do it there.
if youre still not satisfied you can get the artifact name in a maven pom.xml file by using ${project.build.finalName}. the default is ${artifactId}-${version} as you can see here (look at the super pom). if you need it with the suffix it'll be something like ${artifactId}-${version}.${packaging}
if you are running on linux based systems something like
jar -xvf `ls <path>/package-*.jar`
will do the job.

Resources