Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 397
Tom Avatar asked Jun 01 '11 19:06

Tom


People also ask

What is shared library in POM XML?

In essence a shared library is basically a JAR containing a set of JARs and a JBI. xml. The important bits about the Shared Library is that it is named and versioned.

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.


1 Answers

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
like image 87
McDowell Avatar answered Sep 21 '22 13:09

McDowell