Publish Maven build to Artifactory from Jenkins - java

I have a build setup in Jenkins, which compiles a simple Git hosted Maven project into a jar and publishes the jar into Artifactory.
How can I setup the build job to tag the sources which were used for the build with e.g. a build number (or similar), in order to later be able to identify which sources went exactly into this particular build. This build tag should also be visible in git's remote repository, not just in the local version on the Jenkins build server.
Ideally I'd also like to package this "build tag" into the jar (I suppose in a file inside the jar) so I can always correlate the jar to which source files were used to create this jar.
Your help is much appreciated.

Just use the Jenkins Artifactory Plugin or the Maven Artifactory Plugin.
Both generate a BuildInfo metadata that include the information you need and much, much more. All this information is attached to the artifacts and make the artifacts traceble without need to embed this info in the filenames or into the archives themselves.
Please take some time to watch this screencast. It explains in detail what you can get and why it's important.

How can I setup the build job to tag the sources which were used for the build with e.g. a build number (or similar), in order to later be able to identify which sources went exactly into this particular build. This build tag should also be visible in git's remote repository, not just in the local version on the Jenkins build server.
This can be done using the standard Git plugin. Underneath where you configure the repository to clone, click "Add" on "Additional Behaviours", and choose "Create a tag for every build"
Ideally I'd also like to package this "build tag" into the jar (I suppose in a file inside the jar) so I can always correlate the jar to which source files were used to create this jar.
This tag should then be available as an environment variable during the build steps (I think it's called GIT_TAG) - you can have your existing build step put this value into the file or add a pre-build "Execute Shell" step which does something like this:
$ echo $GIT_TAG > git.tag

Related

Pre-download all dependencies

I need to release our Maven build Java project to an remote QA team. For this I would like to download all the dependencies, and send them so they do not need to download them.
Currently all dependencies are defined in the pom.xml file, and we use either mvn install or mvn package to build the project. Some of the project members use uber jars, others use jars + dependencies to do execution.
What would be the easiest way to pre-package the dependent jar files so that there is no download from the internet, and does not change our current build process too much?
A possible solution would be to purge your local repository, tell Maven to download every dependencies and plugin dependencies of your project and make a ZIP of that.
To purge your local repository, you can simply delete the folder {user.home}/.m2/repository. Then, you can use the dependency:go-offline goal:
Goal that resolves all project dependencies, including plugins and reports and their dependencies.
mvn dependency:go-offline
This will download everything that your project depends on and will make sure that on a later build, nothing will be downloaded.
Then, you can simply make a ZIP of {user.home}/.m2/repository and send that to your Q/A team. They will need to unzip it inside their own {user.home}/.m2/repository to be able to build the project.
Offline Package deploy
Your requirement can be accomplished by creating a stand alone jar file with full dependencies. You can port it anywhere please refer https://stackoverflow.com/a/35359756/5678086
Build a full dependency JAR file as said in the answer
Copy the JAR to the destination machine you want
Run the below command from there
mvn install:install-file -Dfile=<path-to-file>
This will install the dependecies in the maven repository of the destination machine. This is fully offline
Theoretically if you know which maven commands you'll use (package, install, etc.) you could clear out your ~/.m2/repository folder, run those commands once on somebody's dev box, then distribute the repository folder. You can run maven -o install etc. to have it not give annoying warnings. This might be a slightly smaller distro than the go-offline answer.

maven - create archive of generated jar files

I am currently building out some projects using an aggregate pom. It calls out some parent poms that build out the artifacts.
The artifacts build out ok.
What I would like is to capture the generated jar files and place them into a directory as they are created build after build, version after version.
Possible?
Thank you!
Without knowing more about why you want to achieve this, it's hard to recommend a solution.
A mvn clean install will install all the artifacts into your local repository, but assuming you want a more custom directory layout - you could use the maven-antrun-plugin and use ant to arrange the relevant files.
If you're happy with the default repository layout, you could also configure the repository settings for the project to deploy your plugin into a specific filesystem location (which would be dedicated to this particular project).
Alternatively, you could use the wagon plugin to do the deployment:
http://www.java-tutorial.ch/maven/deploying-in-file-system-using-maven
im using a .bat file that runs
cd dir
mvn clean install
copy dir\*.jar %LIFERAY_TOMCAT_HOME%"\..\deploy
what do you think? would that suit your needs?

How to download all possible maven dependencies so they are local

I work behind a very massive firewall that likes to hiccup on random connections, which makes all work with remote repositories a living nightmare for me!
I am looking to work with a project from Git (this one https://github.com/mrniko/netty-socketio) which heavily utilizes maven for downloading dependencies.
What I would like to do is on another terminal (such as http://cloud9.io or something) download all the maven dependencies so that the entire project can be run standalone.
I have already tried mvn clean install and then zipping up the source folder, but its actually not enough! I still get ClassNotFound related errors when I try to run the project locally in eclipse. And for the record, I did add the compiled *.class files in the build properties, so Eclipse knows where they are. It seems like there are some random classes that get generated dynamically which still aren't present (such as log4j -- and I really don't want to hunt each one down individually)
I am wondering if there is a fully thorough way to download all possible dependencies from maven and then either run a project 100% standalone, or create a local maven server from it?
I am running Java 7 on Eclipse Luna and I do have Maven installed on my windows 7 machine (though again it barely works on remote repositories). I also have a Cloud9 instance which I could use to run Maven commands, then zip up the results for local download.
When you execute mvn clean install, maven downloads all dependencies of currently built project to your local maven repository. This is usually located in
%USERPROFILE%\.m2\repository
When you build your project, maven uses that path, to lookup required dependencies.
If you want do download them all, you can try using mvn dependency:copy-dependencies. Then, you'll find all project dependencies intarget/dependencies directory of your project. This also includes transitive dependencies.
To add them all as eclipse dependencies, you may want to try maven-eclipse-plugin. Using that plugin, you can generate eclipse .project and .classpath files, using mvn eclipse:eclipse command. This will populate eclipse files with required dependencies from maven. You should then import the project to eclipse using Import existing projects into workspace, instead of Import existing maven projects.
maven-eclipse-plugin will add all those jars relative to a folder specified by M2_REPO variable. Just make sure you edit this variable inside eclipse project properties, and you should be set.
I've had to deal with similar issues. I would find that due to changes in firewall policies, occasionally all the .jar files in my project had been updated to be a 1K file that, when opened within notepad++ contained a message from the firewall saying that the download had been blocked.
I recommend looking into Nexus for your local repository management, it means your local projects don't have to go past your firewalls to check for maven updates.
http://www.andrejkoelewijn.com/blog/2010/03/09/getting-started-with-nexus-maven-repository-manager/
Use dependency plugin go-offline task.

How to include library jar in git project

We have a core set of java libraries, published as jar files, that are used in multiple client projects. Both the library projects and the client projects are on our git server (GitLab EE). Sometimes clients and third-parties (such as sub-contractors) have access to the project source code, but we don't want them to have access to the source for the core java libraries.
So what I need to know is how to set up some kind of dependency or similar in git so that we can automatically pull in the latest version of the library file? I've been looking at git submodule, but that looks like it will pull in, or at least give access to, the full source of the library projects. I've also started looking at Maven, but I'm unclear whether that also needs access to the whole repository. Is there another/better way to do it? Or will Maven actually do what I want? Or do I need to publish my jar files somewhere else, and if so how can I automate that process, as I don't want to have to manually copy the jar file somewhere every time it's updated (which is often)?
Maven and a binary repository like Nexus is the common way to do what you're asking. Nexus stores binary files like .jar's .dll's etc and Maven has the capability to publish and retrieve specific versions of these binaries and include them when building the project. As Ryan commented, Maven Central is a public binary repository that you could publish to if you wanted the whole world access to your .jars, but with Nexus you have fine grained access control over your binaries.
If you build your own projects with Maven, you can configure it to build, test, and publish right to your Nexus repository with a single command mvn deploy. Then clients just have to update their project to point to the new version and Maven will download it and use it on the classpath. It's all very seamless, and I would definitely recommend refactoring your build process to use Maven or a similar product.

Execute Maven goal from Jenkins to create artifact

I have a Maven project which includes in the resources a set of database scripts relating to features which have been implemented.
I've created a separate Maven project which produces a jar file used to create a diff between the database scripts and what has been implemented in the database. This executable takes a command line argument which specifies the database details and the output path for the diff file.
What I'd like to do (and I'm open to suggestions on whether this is the best way to tackle this) is to include the diff jar in my project and to execute a goal from Jenkins which executes the jar and stores the diff file as an artifact of the build.
Could anybody suggest a way to do this as I'm not too familiar with Jenkins.
If this is a jar you can execute, you can just use the java exec plugin.
Once you've got that working to generate the output then you need to set up maven to run the command line that generates that output. This is pretty straight forward. Just add it as a build step.
Then to make it available as an artifact, tell Jenkins to archive the artifact by pointing it to the path of the diff.

Categories