How to change JAR build path in Jenkins? - java

I am building a Java project via Jenkins. The JAR is being created (after I execute maven clean install in Jenkins) in C:\Windows\System32\config\systemprofile.m2\repository\com\other folders.
Is there any way to change this path? I want to sent the JAR to a VM via SSH. However, I am not able to access this folder since this is system directory.
Thank you.

For some reason, your Jenkins uses this directory as local Maven repository. I would define a different local repository in Jenkins or in the settings.xml. It is also important that you make sure that no two running builds use the same local repository at the same time because local repositories cannot be used concurrently.

If you are using maven to build, add the below to your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>your custom location</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>

By default, maven should create your build artifacts (including the JAR file) in target directory next to your pom.xml.
You can change this behavior in pom.xml by specifying the build directory:
<build>
<directory>/some/other/dir</directory>
</build>
The directory you are referring to is your local maven repository, which contains all JAR files maven is working with. You can read more about local maven repository here.

Related

Maven : How to package external jars(folder) in Maven project

I am having a group of external jars(in hundreds) which I have added in the build path of my project to make it work.
But while packaging it is failing as these jar's are not available to maven.
I have gone though many articles and all the solutions(like adding the jar at system path) are for a single jar only.
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Is there any way we can add the group of jars(folder) to the packaging on the project? or any other solution by which my project can build successfully?
can we create a single jar containing all my jars inside and then use the system scope of maven
I have tried creating jar by jar -cvf my_jar.jar * and placed this jar in the system scope. But it does not worked for me.
My solution : Maven pluggin addjar let us add all jar at a place(projectdirectory/lib in this case).
this enables you to add these jar's in the final package(jar in my case) when you maven build, but to run locally you have to add those jar files directly in the classpath.
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Now create a shade jar using mvn clean install shade:shade
The bad news: For a proper Maven project, you need to add each and every artifact you use as <dependency> in your POM.
The good news: I very much doubt that these 100 jars are all
- directly used in your source code
- not available in a public Maven repository like MavenCentral
So the better strategy would be to figure out what you really need and find that in MavenCentral. Then Maven finds all the transitive dependencies for you. So if you really need 10 of the jars and all other jars are just dependencies of your dependencies, just add these 10 ones (from MavenCentral) and you are done.

Adding Jars (More than One) to maven Classpath is giving errors

There are multiple jars (10 jars) and I have to use them in classpath in maven project.
These jars are available in my Project-dir/lib folder.
To handle this I tried
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>lib/*.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
changed lib/*.jar to
1. lib
2. /lib
etc but nothing seems working
Also tried
<repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>lib/*.jar</url>
</repository>
</repositories>
But always faced error (cannot find symbol) on running mvn install
If you want to use jars as dependencies for build or test, they should be inside a Maven repository (not in a lib folder in your project).
The best thing would be to upload them to your company Nexus/Artifactory (if they are not already available from MavenCentral or some other remote Maven repository).
Alternatively, you can use mvn install:install-file to install them to the local repository on your computer.
After that, you can reference them in the pom.xml as dependencies.
Also see: How to add local jar files to a Maven project?

Migrate maven project in offline environment

I have created a web application in spring mvc and now i have to migrate the project to an offline machine but when i imported the project in my eclipse(offline) then it got stuck on "Importing Maven Projects" and also it is showing an error in my pom.xml file at tag :-
<plugins>
<plugin> // shows error over here
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
I also read something about mvn dependency:go-offline so I tried it but couldn't exactly understand that where it saves the dependencies?
So, is there a way through which i could import the project in the offline environment?
As pointed out by #Thomas, the only way to build a project which needs external dependencies with Maven is to copy the repository in the target environment. Build your project on an online environment, then copy your .m2 folder in the offline one. The build will then find all required libraries locally.
With mvn dependency:go-offline you tell maven to download all the needed plugin dependencies in your local repository.

Deploy maven project in to AEM?

I have created a maven project with content and bundle folder i can build the project successfully in eclipse
use this command :-
mvn clean install
but my bundle jar and content zip not reflecting in AEM now i am manually uploading the zip and jar in to AEM but i need to deploy directly from eclipse with out manual intall.
Can anybody help on this ?
install is a phase in the Maven lifecycle during which an artifact is installed in your local Maven repository.
It usually has nothing to do with installing anything in AEM. You need to use specific Maven plugins to achieve that.
If you generated your project based on the Adobe archetype, you need to specify, using a profile, that you want your app deployed.
mvn -PautoInstallPackage install
This profile activates the Maven Vault Plugin and uses it to upload the CRX package to AEM. Here's a snippet from Adobe's AEM archetype where this behaviour is defined.
<profile>
<id>autoInstallPackage</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.day.jcr.vault</groupId>
<artifactId>content-package-maven-plugin</artifactId>
<executions>
<execution>
<id>install-package</id>
<goals>
<goal>install</goal>
</goals>
<configuration>
<targetURL>http://${aem.host}:${aem.port}/crx/packmgr/service.jsp</targetURL>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
Check out the official documentation for more information.
If, by any chance, your project happens to be using the Maven CRX Plugin (Adobe's archetype and its particular choice of plugins is just one of the options available in the wider AEM community), you need to explicitly invoke the crx:install goal.
mvn install crx:install
The bottom line is, mvn install just takes care of installing artifacts in your local Maven repository. In order to deploy to AEM, you need to invoke something more or set up your project to activate some plugins automatically in a certain phase of the lifecycle.

maven install and deploy 3rd party dependencies with simple command line

We have a number of third party dependencies that aren't hosted anywhere. For each of these we have a jar file that we'd like to be able to install and/or deploy to our repository. Some of the jar files have their own dependencies and we also need to declare these.
We've made pom.xml files for each jar file that declare the groupId, artifactId, dependencies, etc. These pom.xml files all have a common parent pom that declares some of the common info (e.g. <repositories> and <distributionManagement>).
I'd like to be able to install or deploy these dependencies with something as simple as mvn install and mvn deploy (or maybe mvn install:install-file and mvn deploy:deploy-file) and have all the necessary properties for these commands (artifactId, repositoryId, etc.) be read from the pom.xml files.
To get this to work, at least for deploying, I tried putting the following in my parent pom:
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<file>${jarfile}</file>
<pomFile>pom.xml</pomFile>
<repositoryId>our-id</repositoryId>
<url>our-url</url>
</configuration>
</plugin>
</plugins>
</build>
and then having each of the child poms define the jarfile property. That allows me to run mvn deploy:deploy-file to deploy all the child pom artifacts. Presumably I could do something similar to get mvn install:install-file to work.
But with this approach, I'm unable to release the parent pom (which I must do since the child poms depend on it), and if I try to mvn release:perform on the parent pom, I get errors like:
Cannot override read-only parameter: pomFile
I feel like I'm probably going about this the wrong way. All I really want to do is:
Put the common code for all the third party jars in one shared parent pom
Write an additional minimal pom for each third party jar
Be able to run something like mvn install or mvn deploy without having to specify all those complicated command line properties
How can I best accomplish that?
Edit: Made it clearer above that ideally I'd like to be able to run something as simple as mvn install or mvn deploy and not have to specify properties on the command line.
When Maven is missing a dependency, it will give you an error in it's output that contains the command line to issue to add the jar to a remote repository. That command line will automatically create a POM for you and upload the two together. Is that not sufficient for your needs?
Using this facility, if I know that I have a dependency with no remote repository representation, I usually just go ahead and define it in my build with the GAV that I want, run the build once, then look at the build errors. Copy out the command for the deployment, then run that command. You'll want to make sure that you have the <repository> elements set up properly in the parent POM plus also set up corresponding <server> elements in your settings.xml with the repository upload password. Other than that, you should be good to go.
I would add that you should check out Nexus before getting to far. It's worth the hassle to set up now! :-)
Ok, I found a solution that allows me to run just mvn install or mvn deploy and have the jar file installed to the local or remote repository. Inspired by a post to the maven-users list and using the build-helper plugin, in the parent pom, I have:
<pluginManagement>
<plugins>
<!-- Attach the jar file to the artifact -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${artifactId}-${version}.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
And then in the child poms, I have:
<packaging>pom</packaging>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Some of the pieces of this that initially tripped me up:
The attach-artifact execution should be under <pluginManagement> so it doesn't get executed if you mvn install or mvn deploy the parent pom.
The children need to specify the build-helper-maven-plugin under the build plugins so that code from the parent <pluginManagement> gets run.
The children have to be declared as having <packaging>pom</packaging> because you can't add a jar to an artifact if it has the same name as the artifact.
The only downside I see to this approach is that the artifact gets deployed as type pom instead of type jar. But I haven't seen any real consequences of that.
I meet this problem in my work:
Now I have a target.jar (it has a dependencies list : a.jar, b.jar, c.jar...), I want to use mvn install:install-file to put it into my local repo, but when I run the command blow
mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance -DartifactId=target -Dversion=1.0.0
but when I use it I found there are many error, the jar which use target.jar cannot find a.jar, b.jar, c.jar, such as:
com.cnetwork.a does not exist
com.cnetwork.b does not exist
com.cnetwork.c does not exist
Then I went to ~/.m2/repository/.../target/1.0.0/target.pom to find the pom file of the target, but nothing in it!
...
<groupId>com.cnetwork.relevance</groupId>
<artifactId>target</artifactId>
<version>1.0.0</version>
....
# no dependencies about a/b/c.jar !!!
This is what going wrong, the install:file -Dfile -DgroupId -D.. does not add dependencies into pom, I correct the answer by this method
if you already have this maven project source, just install it into local repo
mvn clean install
if you have the jar and the pom of it, install jar with pom
mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom
if you don't have a pom with target jar, write one and use upper command.
if you cannot recover the pom file of it, may be you should give up.

Categories