Transitive Dependency How do i exclude. -- Maven - maven

I have the below project structure under lib for project 2.
Project 2 (under lib)
- Maven dependencies (JARs)
- project 1 JAR (it has JARs in lib)
- Maven dependencies of project 1 (JARs)
I want to exclude all JARs under project 1 while preparing project 2.
I'm currently using the below in my POMs (both project 1 and project 2)
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
When building project 1, I want it to build as an executable JAR with lib.
However when building project 2, I want project 1 as only a compiled JAR (meaning with only class files and not having lib (JARs)).
Project 1 is included as a normal dependency in Project 2. Can anyone help me out?

If you want to exclude all dependencies of a given dependencies, use exclusions
https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html
and do it like
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
Of course, you have to be sure that the missing transitive dependencies are not needed at runtime.

If I understand correctly project 1 builds a jar which has inside a lib directory with the dependencies.
You could in project 1 have 2 assembly executions, one the basic jar and another with dependencies and execute manifest, normally you use a different "classifier" for multiple artifacts of the same project.
Otherwise you would need to "repackage" the jars to remove the libs you don't want using some plugin like Shade.
I think the first option is much better, also by default if you publish it to a repository both jars will be published so other people can get one or the other.

Related

How to load classes from target instead of from jar in maven pom

We have a multi module maven project where previously we load our classes from a jar dependency as below
<plugin>
<groupId>xxxx<groupId>
<artifactId>xxx<artificatId>
<version>xxx</version>
<dependencies>
<dependency>
<groupId>yyyy<groupId>
<artifactId>yyyy<artificatId>
<version>yyy</version>
</dependency>
.
.
</dependencies>
</plugin>
Now due to business requirement,
I want remove dependency (yyyy) and use that dependency code as part
of project/src folder
No more dependency used -- business condition
3.. Plugin XXX has to load classes from project/target of project.
Issue here is when plugin xxx is running it is looking for classes which are defined as dependency but not from project target folder.
Please suggest how to proceed ?

Excluding transitive dependency not working

Project A references Project B. Project B has included a local dependency. This local dependency unfortunately has a dependency to net.java.dev.designgridlayout in version 1.5.
We want to use net.java.dev.designgridlayout in version 1.11 in Project A but we are not able to "overwrite" the dependency. Eclipse always uses the dependency from Project B.
We already tried to exclude the 1.5 version from the local dependency, but it doesn't work.
The strange thing is, that Eclipse successfully resolves a class that has been added with version 1.11. For an already existing class, however, eclipse resolves it from the transitive dependency from de.someCompany.
Project B:
<dependencies>
<dependency>
<groupId>de.someCompany</groupId>
<artifactId>fs-client</artifactId>
<version>5.1.209</version>
<exclusions>
<exclusion>
<groupId>net.java.dev.designgridlayout</groupId>
<artifactId>designgridlayout</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.java.dev.designgridlayout</groupId>
<artifactId>designgridlayout</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
Project A:
<dependencies>
<dependency>
<groupId>Project-B</groupId>
<artifactId>Project-B</artifactId>
<version>1503.01</version>
</dependency>
</dependencies>
I also tried to include the 1.11 dependency in Project A.
We even tried to install the DesignGridLayout V. 1.11 in the local dependency and to change the groupID and artifactId to something different, but it cannot even be found by Eclipse for some reason. If it would be possible to include the DesignGridLayout with another groupId and artifactId, I think it would work.
mvn install:install-file -Dfile=lib\designgridlayout.jar -DgroupId=com.company.designgridlayout -DartifactId=design-grid-layout -Dversion=1.11 -DgeneratePom=true -Dpackaging=jar -DlocalRepositoryPath="%USERPROFILE%\.m2\repository"
Not sure - but:
Your project A has a dependency to itself? Shouldn't it use project-b?
Its not a good idea to change group or artifact id's as maven can no longer detect its the same artifact. If you do a custom version the version number should be enough.
If you add the dependency in your own pom then you don't need to exclude the artifact, since the groupId and artifactId are the same. The version in your own pom will win in project-b. If project a defines that dependency again itself that version will win.
I would do a mvn dependency:tree on project-a pom to see where the dependencies come from.
For eclipse: it indexes the local repository. In the maven settings there is a re-index button. So if you manually copy jars in there that may help eclipse to find the artifact. But that workaround would need to be done on every machine. I would not count that as solution. In the maven world artifact-resolution is an infrastructure issue and should not be handled per project. The way this is done should be transparent through the settings.xml

How to add all dependency in my project pom file?

I have added around 100 jars in my local Apache Archiva. Now i will want to add all these dependency jar to my project Pom.xml file.
Can it possible to add all these dependency by single Copy-paste? Right now i have to copy each individual dependency from Apache Archiva and paste into my project pom.xml file.I have to copy-paste these lines in my Pom.xml file for each jar which is very tough task.
<dependency>
<groupId>org.csdc</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
It's very unlikely that you need all 100 jars as direct dependencies. In maven, you have to list your direct dependencies - one by one, yes. However, you don't need to list your transitive dependencies because maven will manage that for you. This is one of the most fundamental improvements over older manual classpath management java building.
No All dependency of all jar,
because of in that jars some of the dependency have same group Id ,
so that have fetch all the jars that included.
some of the dependency is writing in pom.xml file
for example code is
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
the above dependency fetch all jars of related to hibernate-annotation
- hinernate-annotation
- hibernate-common-annotation
- hibernate-core jar files to be fetched.....

Maven is picking up the oldest snapshot in Archiva - why?

I have a projectA-parent with the following structure:
projectA-parent|
----------------|projectA
----------------|projectA-core
----------------|projectA-api
The module projectA simply defines dependencies to core and api [1], so I can just define a dependency to projectA in other projects that need both the core and the api. I deploy the project to Archiva (1.4-M2), to a SNAPSHOT repository.
I have another project X where I define a dependency to projectA, and this afternoon the goal "mvn compile" started failing with unresolved compilation issues. Upon investigation with "mvn dependency:tree" on that project I discovered that, under projectA, the dependency to projectA-core was declared as "runtime". While this morning it was like this, I performed several mvn installs since where the scope is "compile" (see [1], where the most recent pom definition is listed).
Looking at archiva, I see the following files for this project (abbreviated):
projectA-0.0.7-20120712.084920-61-tests.jar
projectA-0.0.7-20120712.084920-61-tests.jar.md5
projectA-0.0.7-20120712.084920-61-tests.jar.sha1
projectA-0.0.7-20120712.084920-61.jar
projectA-0.0.7-20120712.084920-61.jar.md5
projectA-0.0.7-20120712.084920-61.jar.sha1
projectA-0.0.7-20120712.084920-61.pom
projectA-0.0.7-20120712.084920-61.pom.md5
projectA-0.0.7-20120712.084920-61.pom.sha1
projectA-0.0.7-20120712.172412-87-tests.jar
projectA-0.0.7-20120712.172412-87-tests.jar.md5
projectA-0.0.7-20120712.172412-87-tests.jar.sha1
projectA-0.0.7-20120712.172412-87.pom
projectA-0.0.7-20120712.172412-87.pom.md5
projectA-0.0.7-20120712.172412-87.pom.sha1
projectA-0.0.7-20120712.180733-90.pom
projectA-0.0.7-20120712.180733-90.pom.md5
projectA-0.0.7-20120712.180733-90.pom.sha1
Here's the interesting part: if I delete projectA from my local .m2/repository, even if I "mvn compile -U" I will get th 08:49 version, not the 18:07 one! This means either maven or archiva are resolving the 1st sNAPSHOT of the day and not the most recent one.
Why is that, and how can one solve this?
[1]:
<dependencies>
<dependency>
<groupId>com.projectA</groupId>
<artifactId>projectA-api</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.projectA</groupId>
<artifactId>projectA-core</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
</dependencies>
What is the packaging of your projectA ('jar' or 'pom'). It only exists a 'jar' artefact for 08:49.
I suppose you changed the packaging to 'pom'. Thus if you declare the library as dependency in other projects you have to set <type>pom</type>.

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