Adding A Group of Files to a Local Maven Repository - maven

I have an existing java project. It is a maven project. I want to add it to my local maven repository for reuse in other projects.
MyUtilityClass.jar
MyUtilityClassSources.jar
MyUtilityClassDocs.jar
Dependent on
slf4j-1.7.2.jar
junit-4.11.jar
This SO Question/Answer gives instructions for adding a single file to a local maven repository using mvn install:install-file
This SO Question/Answer gives instructions for adding a group of jars - sources, javadoc, etc. using mvn deploy-file.
This SO Question/Answer alludes to specifying dependencies via a pom.xml file, but doesn't provide details.
I have two tasks in front of me that I'd like to accomplish:
add the project for MyUtilityClass to my local repository, with sources, docs, and dependencies automatically added when I pull them in.
I'd like to create a maven task for the MyUtilityClass project that adds the latest and greatest bundle to my local repository.
I am using eclipse as an IDE and maven version 3.0.4. The POM for this project looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.local.mine</groupId>
<artifactId>MyUtilityClass</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyUtilityClass</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.local.mine</groupId>
<artifactId>slf4j</artifactId><!--from local repo -->
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>com.local.mine</groupId>
<!--from local repo and shouldn't be automatically added -->
<!-- to projects that reuse the resulting bundle -->
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>

The easiest way to do this is to think of each project as their own release cycles. They should be independant of each other.
As you have discovered, you can
use install to install the artifact to your local repository
use install-file to install secondary artifacts to your local repository
use deploy to so the same steps as install(-file), but to remote repositories
What also does this is the release plugin. When you perform the pair goals of release:prepare and release:perform, you end up with
version values update in the pom files
version control tags created
the artifact installed & deployed
any source and/or javadoc artifacts deployed
What you want to use is the release plugin.

The simple solution is to use
mvn install
in the project you would like to reuse. This will install the artifacts of your project into your location repository.

Related

Jenkins couldn't pull the jar from private artifactory with -SNAPSHOT

I can build it locally with no problem, but during jenkins build, i am getting error for not able to find the my-app.jar. I do have the SNAPSHOT fold in my artifactory site.
<dependency>
<groupId>com.my.company</groupId>
<artifactId>my-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Once I changed my pom to call the latest jar in the SNAPSHOT folder, everything started to work again.
<dependency>
<groupId>com.my.company</groupId>
<artifactId>my-app</artifactId>
<version>0.0.1-20181204.170129-19</version>
</dependency>
Should the artifactory return the latest jar automatically when I use -SNAPSHOT?
The first one should work fine. I guess you have a caching problem. Try to delete the relevant directories from the local repository and try again.

Jenkins fails to build multi-module Maven project

I have a multi-module Maven project where I have multiple micro services as modules so I have modules listed in my parent pom.xml like below:
<modules>
<module>core</module>
<module>model-base</module>
<module>module1</module>
<module>module2</module>
...
<module>module5</module>
<module>module7</module>
<module>module6</module>
</modules>
Here the module7 is dependent on module5, 6 so I have dependencies listed like below in my module7 pom.xml:
<parent>
<artifactId>pojectA</artifactId>
<groupId>com.domain</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module7</artifactId>
<dependencies>
<dependency>
<groupId>com.domain</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module5</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module6</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
When I run mvn clean package in my local the module5, 6 called before the module7 as expected but in Jenkins it is trying to build module 5 then module7 making build fail saying:
[ERROR] Failed to execute goal on project module7: Could not resolve dependencies for project module7:jar:1.0-SNAPSHOT: Could not find artifact module6:jar:1.0-SNAPSHOT -> [Help 1]
Do I need to run any other jobs or re-order the modules in my pom.xml, how is it differ from local to Jenkins? Appreciate any help on this.
The order of modules is not relevant. Maven recognizes which project depends on which other project(s) and sets the build order in the reactor accordingly. See POM Reference, Aggregation (or Multi-Module):
You do not need to consider the inter-module dependencies yourself when listing the modules, i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.
Add Pre-Step as per below attached screenshot. This will compile all your top modules.
Then we can execute which ever module we want.
As is probably quite well understood, the issue is that the dependencies between the child modules fail because they aren't installed in the local repository yet (because they are yet to be built). The goal that causes this (for me anyway) is mvn test, which is invoked by mvn package. Your local build probably works because at some point you've done a mvn install and this has bootstrapped your system.
In Jenkins the only way I've found to make these builds work is to use the Pre-build step invoking a Maven target of install, and then build the main step as usual.

Maven copy parents declared in the POMs

My final goal is to create a Maven repository in a certain directory containing only a certain set of artifacts and all their dependencies.
For this I use the following command:
mvn.bat dependency:copy-dependencies -f dependencies.pom
-DoutputDirectory=localRepoDir -Dmdep.useRepositoryLayout=true
-Dmdep.copyPom=true -Dmdep.addParentPoms=true
dependencies.pom being:
<project>
<modelVersion>4.0.0</modelVersion>
<description>Dependencies</description>
<groupId>com.dummy</groupId>
<artifactId>dummy</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.dependency1</groupId>
<artifactId>dep1</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.dependency2</groupId>
<artifactId>dep2</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
When doing this, I notice that parents declared in the dependencies' poms are not copied from the .m2 Local maven repository to the destination directory.
Perhaps I'm missing something and there's a better way to do this, since it's kind of a hack to use a pom file to declare the artifacts I want to copy (together with their dependencies).
Turns out that maven was using version 2.8 as default for the dependency plugin. When explicitly indicating it to use the latest version (2.10), it worked just fine.
The addParentPoms parameter was already introduced on 2.8 for copy-dependencies, so I guess it must be a bug in the 2.8 release.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies

how to add my one project jar in another maven project in netbeans

I have one project jar oauth.
I want to add it in another maven project . I tried to change pom.xml file but no effect. Can anyone please suggest me?
I tried to add following dependency in my pom.xml file:
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapps/WEB-INF/lib/auth.jar</systemPath>
</dependency>
With the assumption that you have that auth.jar in your local repository (as it builds fine).
Why don't you give a try like this.
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
Honestly speaking I don't prefer to give the jar location in my pom file and using scope as system, I leave this task to handle by Maven to resolve all the artifacts either by searching in local maven repository first(/.m2) or in MAVEN CENTRAL REPO if it is a 3rd party jar.

Maven - Sharing libraries between projects

I'm working on a multi-project, and right now I have a structure that resembles this (actually there are a couple of jar projects and a couple of war projects)
/myProj
|_______projA (jar)
| |____pom.xml
| |____target/jar files
|_______projB (war)
| |___pom.xml
| |___web-inf/lib/jarfiles
|_______projEar
| |___pom.xml
|___pom.xml
What I want to achieve, is to make projA and projB to read their dependences from a common shared folder, instead of keeping their own copy.
Actually, I don't really care where they read them from at compile time, but when I package my EAR file, I want each jar/war to appear just once, hence reducing the EAR size.
I've tried declaring the dependencies on the parent pom, declaring the dependencies as and some other things, but so far I haven't achieved this.
Is there an easy way to achieve this? Any simple maven plugin?
Thanks in advance.
You should be able to do this by adding the JAR as a dependency to your EAR's pom.xml:
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myapp-web</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myapp-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
...and specifying the dependency as provided in your WARs' pom.xml:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myapp-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
If Maven/other tooling has already copied the JAR to your WEB-INF/lib directory, you may need to delete the file manually prior to rebuilding.
This should result in an EAR of the form:
META-INF/MANIFEST.MF
lib/myapp-utils-0.0.1-SNAPSHOT.jar
META-INF/application.xml
myapp-web.war
When you are moving to Maven, you should not store the dependency JAR's in your code base. I would suggest you to create a central Maven repository which will contain all the dependencies.
Refer mvn install to first install these artifacts into the local repository. Also, you can refer to the maven central repository to get artifacts while building.
What you need to do is: remove all the dependency jar's from the source code, and all their dependency in the pom.xml. These would be downloaded and packaged from the maven central repository as and when required. Set the Dependency Scope of the artifacts accordingly.

Resources