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

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.

Related

Setup maven pom to work with dependencies across environments

I have a Java projects a-1.0.jar with ojdbc.jar dependency and b.jar that depends on a-1.0.jar and ojdbc.jar. I am trying to make it work on my machine, new user machine and a Bamboo server.
Desired behavior:
On local machine git clone <git_url>, mvn clean install, java -jar b.jar project should run. Bamboo should checkout and run project.
On Bamboo: a plan can check out a project and run it. Build should track version of b.jar built and a.jar used.
So far I saw these options:
<systemPath>${project.basedir}/lib/a-1.0.jar</systemPath>: maven warns that it will fail to resolve dependencies
A Perl script to run mvn install for each dependent jar before building the project
(1) defeats the purpose of DevOps automation
(2) makes it unclear which version of a jar was used
(3) installs the jar, but java -jar b.jar fails a.jar is missing
I can overcome this with another Perl script that adds the dependent jars to a classpath
These are basic tasks and as a build tool Maven should be able to do it.
How to tell Maven to three goals below?
(1) For each unknown import - get a jar from lib folder
(2) Make a set of dependent jars. That is don't import ojdbc twice
(3) Pack a self sufficient jar that runs, not fails with "stuff is missing"
Seems like you need to create an executable jar - and for this, you can use various approaches.
One of them is, add maven-shade-plugin which puts all dependencies into single jar, while taking care of potential resource collisions.
Try non-maven-jar-maven plugin. It adds jars that are not in the maven central.

Run mvn exec:exec without a pom.xml

I am trying to find a way just to fetch a library from maven repository and run it. I want to define everything by command line and ignore the pom. I am trying following:
mvn exec:exec -Dexec.mainClass="org.main.Class" -Dspring.profiles.active=test
When I try to run it with pom.xml, it starts to fetch all the dependencies described in the pom. Which I really don't want. When I run it without the pom.xml, it says Goal requires a project to execute but there is no POM in this directory. Please verify you invoked Maven from the correct directory. Is there a way to run it without the pom or at least ignore it?
My goal is just to start my application from anywhere without sources or jars.
One possible workaround would be to:
first get the jar
then execute it
For that, the maven plugin dependency:get is handy, and can be executed without any pom.xml/project (so from any folder you want)
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get -DrepoUrl=https://dtp.intramundi.com/nexus/repository/maven-central/ -Dartifact=<group>:<artifact>:<version>
Once you have copied the artifact to the folder of your choice, a plugin like dependency:copy-dependencies could, reading the pom.xml of the artifact you just got, copy said dependencies to the same folder of your choice.
Finally, a java -cp . yourArtifact.jar can execute it.
I realize this is not as straightforward as just "exec:exec", and that it fetches dependencies from the pom.xml, but here at least:
the first step does not require any pom.xml,
the second step needs the pom.xml of the very artifact you want to execute, and those dependencies might be needed at runtime: you would need those anyway if you want to 'exec' your artifact.
Nope. It's impossible. Mvn Exec is, in fact, Maven Pluging dedicated to execute something as part of maven-nature project. If you need to execute Jar outcome why not use just java ***.jar ?

How to get all the specified jars mentioned in the pom.xml and transitively dependent jars?

I have only the POM file using which I need to write a script to automatically download all the dependency files and output to custom mentioned path.
I just want to achieve the above using mvn command in command line.
It sounds like you're looking for mvn dependency:copy-dependencies:
dependency:copy-dependencies takes the list of project direct dependencies and optionally transitive dependencies and copies them to a specified location, stripping the version if desired. This goal can also be run from the command line.
From the project root, invoking on the command line
mvn dependency:copy-dependencies -DoutputDirectory=...
will copy all your project direct and transitive dependencies to the specified output directory. If those dependencies are not already in your local Maven repository, they will be downloaded from Maven Central (or from a custom repository).

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 install default jar file with maven-jar-plugin

Given a project with <packaging>something-not-jar</packaging>, how does one:
tell the maven-jar-plugin to produce the "normal" jar file and tell the maven-install-plugin to take that normal jar file and
install that to the local repository (in addition to the something-not-jar artifact that was produced)
If I tell the jar plugin to produce a test-jar, then it does that and the install plugin installs the xxx-0.0.1-SNAPSHOT-tests.jar to the local repo. But the "jar" goal apparently does not cause the resulting jar file to be installed in the local repo, even though the jar file is created in the target folder.
So how do I do this?
You should simply do the following:
mvn install

Resources