how to use local dependency for docker images? - java

I have docker-compose with the following projects:
RabbitMQ management, producer and consumer,
producer and consumer (java springboot apps) share a maven dependency to common model api (3rd mini project containing POJOs used by producer and consumer).
When I build everything locally with mvn and each dockerfile contains only copying jar and running the app everything works fine.
I tried to use multi-stage dockerfile but there is an error stating my producer can't find dependency for model-api on maven-central (from where it downloads all other dependencies).
I have tried using RUN mvn dependency:go-offline so it uses my local .m2/repo but no success, same error and still downloads dependencies from central.
How can I make it work?

Related

The correct way to deploy artifact from nexus

I am trying to build and CI environment and I think that it works because I can publish a jar into Nexus automatically.
Git
BitBucket
Jenkins
Nexus OSS
I am looking information about the deployment of maven projects automatically when it publish to nexus , but I am not sure about my initial idea.
My initial idea is to download the latest artifact from Nexus3 and use mvn spring-boot:run but I am not sure about my decission.
I read information about Puppet but I am not sure about this option, could anyone help to me about this process ? Or send to me another link in the StackOVerflow about this problem.
Regards !
Build a pipeline in Jenkins:
First job builds an artifacts and uploads it to the storage (in your case it's Nexus). It has to determine which version it uploaded - the easiest way to do that is to set the version by the job itself and then upload the known version (see a sample script at the bottom).
Then pass the known version to the downstream jobs (like deploying to envs). You'd need to tell Jenkins which version you just deployed - it's possible to write it into a key-value file and then use something like EnvInject plugin to read it as an env var.
To download an artifact from Nexus use wget/curl:
http://nexus.domain/nexus/service/local/artifact/maven/redirect?r={repository}&g={groupId}&a={artifactId}&v={version}&p={type}
Jenkins has several plugins for building pipelines, but you can start with a simple built-in possibility of jobs to invoke other jobs.
Couple of notes:
Do not use Maven for deployment. Maven is a build tool, it's not suitable for deployments. Write bash scripts and possibly use Docker for deployment automation.
Do not use LATEST/SNAPSHOT/RELEASE versions - you need to have a deterministic deployment (if it's repeated with the same params it deploys the same artifact).
A sample script that sets a snapshot-like version (so it's possible to upload the artifact to snapshot Maven repo):
VERSION=`mvn help:evaluate -Dexpression=project.version | grep -v "^\["| grep -v Download`
VERSION=${VERSION/%-SNAPSHOT/} #get rid of -SNAPSHOT if it's there
VERSION="$VERSION-"`date +"%Y%m%d.%H%M%S"`"-$BUILD_NUMBER"
mvn versions:set -DnewVersion=$VERSION

Execute jar file through maven by pulling them from a remote repository

I have a maven project which I build and deploy on a private remote repository. The code is built through continuous integration pipeline.
This repository is accessible from my remote machine. What I want is to pull the artifact and execute it on this remote machine without pulling the source code for the project.
From what I can understand, one needs a pom file which contains this repo, as a dependency, but is their any other way of doing the same without having to download the sources
If it is possible for you to have just one file to download pushed to Artifactory (I assume you are using artifactory from the tag) it is possible to download the file with a simple http get request (via Artifactory REST API), like curl, or even use the JFrog cli.
I recommend using the CLI: https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory
To use Artifactory REST API directly go to:
https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API
Retrieve Artefact api or Retrieve Latest Artefact can be helpful.
you can simply download the file using CLI provided that you have maven installed. Below is an example command.
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=<your nexus url> -Dartifact=<groupid>:<artifactid>:<version>:jar:jar-with-dependencies -Ddest=<destination>
Have you tried the exec-maven-plugin ? http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html
That is if you want to do the job with Maven (it is not very clear to me if you do or do not want to use Maven on your remote machine).
Note : repacking the dependencies in your executable jar would not be a good practice. You can still build a distribution archive along with your jar (look at the maven-assembly-plugin https://maven.apache.org/plugins/maven-assembly-plugin/) and use that archive instead.

Android library to work with local maven repository

I have an Android app which uses a library of my own. I am developing them at the same time so when I have a change in my library I want to test it in my app as well.
They are setup as different projects since my library will also be used by other developers. The way we work is we build the library using a CI platform adn deploy it to an artifactory server.
Then from the app I reference this library directly through artifactory. This way when my CI platform builds the app, the build process takes the lib from artifactory and CI works smoothly.
This is a good way to work but is a pain in the ass when developing them in parallel, because I have to commit the changes, create a pull request, merge it with the development branch and wait for CI to build it and deploy it on the artifactory server, just so I can test it on the app.
Coming from java EE development, I used maven install, which deploys the artifact in the local maven repo, then I could already use it from my web application.
I want to do soemthing similar, i.e. have gradle deploy my artifact on my local repo, so the artifact on the local repo is updated but not on the remote one. This way I can debug more easily while still keeping the CI setup in place.
But I have no idea how to do this on gradle. The artifactory plugin seems like it only allows deployment on an artifactory server.
any ideas?
If you're using the maven plugin, you can run the install task to deploy the artifacts to your local Maven repo [1]. After you have the Artifacts deployed to your local maven repo, you need to add mavenLocal() [2] as one of the repositories to be able to resolve the dependency. One strategy I use is to always set a custom version for my local copy so that I can be certain that the local version is getting picked up - but if you choose not to do that the dependencies get resolved in the order the repositories are listed (so you'll need to ensure mavenLocal is before your Artifactory server).
[1] https://docs.gradle.org/current/userguide/maven_plugin.html, https://github.com/dcendents/android-maven-gradle-plugin
[2] https://discuss.gradle.org/t/how-to-use-maven-local-repository-for-gradle-build/2244

Running a multi project Maven-built webapp on Heroku?

I'm migrating my multi project Java webapp from Cloudbees to Heroku. I have one main webapp that depends on 3 other library projects that I have written (also Maven+Java). On Cloudbees this was simple, you just build everything through Jenkins, the JARs get poked into their Maven repos then the main webapp gets built taking its dependencies from that Maven repos.
However, I can't for the life of me find a way of doing this on Heroku without doing something horrendous like this:
https://devcenter.heroku.com/articles/local-maven-dependencies
By doing so it would mean that every time I changed a library I would have to remember to deploy the built JAR to the main webapp project and push this to Git, somewhat defeating the point of using Maven!
I understand that the way to deploy your webapp on Heroku is essentially by doing a git push to their repo, but how can I tell heroku at the other end to find my library dependencies without having to bundle them in the main webapp like this article suggests?
I would assume that there is some way to set up a private maven repos on your heroku account but I can't find anything like this.
I think the best solution is to use a private Maven repository, and then add the config for that repo in the settings.xml of your main project. This article on using a custom settings.xml describes how to do that.
There are some services that host private Maven repos for you, such as JFrog. But I've even read of people using Github to host a Maven repo, too.

Maven/Jenkins automatic download/copy

I'm in a team to develop plugins in java.
We got Maven, a repository and Jenkins,
And I got myself a debian-server to test my applications.
When I push my commits, this happens:
Push in repository, upload and build in Jenkins.
Users download these .jar files and upload these to their server
What I want to be happen:
There are two ways,
First: After building with Jenkins: Download these files,
Second: When pressing "maven build", maven builds my applications and copy these to my server
How can I do this?
(Sorry for my bad English: I'm German)
If I understand you correctly, following scenario should help.
create two jobs
first job builds your artifact and archives it: user will be able to download it, job is triggered by source modification
second job is only executed manually (and requires two plugins, listed below), copies artifacts from first job and then uploads them to the server
Following plugins you would need to install:
copy artifact plugin to enable jenkins to copy artifacts from another job
publish over ssh plugin to enable jenkins to upload binaries and run commands on remote servers
You want when you push your SCM repository, to get Jenkins build into your local projet (update your jars), then manipulate send your final build to your server. Is that you want ?
If yes, I think that plugin would be interesting :
http://evgeny-goldin.com/wiki/Jenkins-maven-plugin#Supported_Plugins
you can manage jenkins goals from your pom.xml but I am not sure if you can get jenkins build.

Categories