How to deploy jar/war to nexus in the docker build? - maven

I currently following this methods for my maven java project in jenkins
created a compose file with maven image, and given a entry point command "mvn clean install"
Created a Dockerfile, From tomcat image. that will copy my war to ../webapps folder
Once the docker compose up is exited with the code 0, I am starting the docker build with the Dockerfile.
Then pushing the image to my registry
Now the problem is. I need to push my war to nexus.
I know , still we can achive by command mvn deploy ....
But is going like all we need to maintain for each and every project.
Previouly I used m2release plugin. that plugin have option to execute the try run and release run. once it done, it automatically change my pom to next vertion and commits to git.
Now I want to achieve that with docker build in jenkins. Or my way of doing is wrong? if yes, what is the correct solution for doing this?

Related

Maven deploy current artifact in target folder as it is

For a web application we have a Jenkins pipeline with these steps:
maven build of the back-end (mvn clean install)
npm build of the front-end (npm run build)
update the back-end .jar file including inside the front-end dist folder (jar -uf ...)
deploy that jar file into our development environment (docker container on OpenShift)
This works very nicely for the deployment. The question now is how to keep these artifacts in our repository (Artifactory). If we use the mvn deploy command in step 1, the artifact we store in our repository will be the jar file without the front-end. What I would like is after step 3 make a call to maven that deploys the jar file in the /target folder as it is without modifying it.
I've seen this other question, but like this I would need to specify many things as version, groupId... what from Jenkins could be difficult and also all this information is already defined inside the pom.xml file.
Would it be possible to call maven to use the already contained configuration and just perform the upload to Artifactory step?
You can probably just call
mvn deploy:deploy
in the end.

Jenkins pipeline re-download maven artifacts again and again when running the pipeline

I need to know why in my jenkins pipeline when creating a docker container for building a java app with maven everytime that I run the pipeline it downloads all the java artifacts/requirements again and again from the pom.xml file,, help please!
Because the directory, where maven caches these artifacts (~/.m2) is inside the docker container and thus not preserved between runs.

Does a Maven packaging type `docker` exists?

I am trying to figure out how to build Docker images as part of the Maven build process. Basically, I want to create a Maven project which builds a Docker image. I think this makes sense.
But what should the packaging type look like? It's obviously not jar, war or maven-plugin. I tried with docker but that does not exist.
Should I just go for pom or is there a way to configure custom package types?
Having it at pom feels wrong to me, as we are producing an output - a Docker image - which is usually not the case in regular pom types (parent pom, bom, ...).
Wow that's ambitious! When you say you want to build a docker image using Maven, I'm assuming you won't be using docker build or other docker commands to generate the final image. Docker images are TAR files so you can use the Maven Assembly plugin to generate the desired tar-file image.
In this case, your project's "type" would be "pom" so that the project itself generates a POM file containing the information about how it was built (such as name and version number). The Assembly plugin will also generate and attach the corresponding TAR file so that when you use mvn deploy or other commands which reference the package, the associated TAR file will be included.
If you are using docker commands to build the images then you are probably going to have to write a custom Maven extension -- depending on what you want to happen when you run mvn deploy. It really depends on your ultimate goal but if you are using this within the confines of Docker then you might do better with a plugin like dockerfile-maven which will allow your build to drive the docker workflow of docker build and/or docker push.
Spotify has created a dockerfile-maven-plugin that allows you to build and publish Docker images with maven. There is an example and lots of other useful info on the project page.
Of today, there's a Maven docker plugin that enables <packaging>docker</packaging>, here:
https://github.com/fabric8io/docker-maven-plugin
Doc here:
https://dmp.fabric8.io/
But take care to specify <extensions>true</extensions>!

maven plugin for docker (e.g. to run with needed db)

Making an image for maven project should be straightforward, as maven know how to build (and can know how to run)
How to build docker image and run it with maven?
Let's say the app also needs MongoDB, that I can run as docker run -p 27017:27017 mongo. Is it possible also to specify with some maven plugin?
The maven plugin created by fabric8 allows you to do this:
the plugin and its documentation is available on github: https://github.com/fabric8io/docker-maven-plugin
The samples include for example https://github.com/fabric8io/docker-maven-plugin/blob/master/samples/data-jolokia-demo/pom.xml (which seems to be similar to what you plan).
An alternative could be using Docker-compose and some scripts outside maven, once the images are created.

How to split maven tasks between tasks and pipelines without running redundant maven tasks

I'm trying to set up a deployment pipeline using GoCD as follows:
Compile, test and deploy to Maven repo
Check out source code from SVN
Run mvn clean compile test install
Run mvn deploy to deploy the WAR artifact to Sonatype repo
Deploy to Tomcat server
Retrieve WAR artifact from Sonatype repo
Run mvn tomcat7:redeploy to deploy it to the Tomcat instance I have running
The thing is, I can't seem to split 1.2 and 1.3 (for example) without having to rerun the whole the entire source code checkout again in 1.3. This seems redundant to me as I had already gotten it up to the package stage and should be able to just continue to run from there.
Between 1.3 and 2.2, I can see that it can retrieve the WAR artifact from Sonatype, but I can't do much with it with maven because there's just no pom.xml for me to execute the maven task with. Of course, I can just add the source code material and run the entire mvn package tomcat7:redeploy cycle again, but I'm pretty sure that's not what this was designed for initially.
I can also write a shell script and ask Go to run it to copy the WAR file to the right location, but again, I could have done everything in one maven pass and save myself some effort but that would just reduce the entire pipeline to a single box which isn't much help to help visualize the deployment pipeline.
Can I get some advice on how I should be designing this pipeline if I wanted to split a maven task flow into different Go tasks / pipelines?
Thanks
Wong

Resources