0

I created a maven parent project, where I have:

Parent pom:

<modules>
    <module>my-project-solution-a</module>
    <module>my-project-solution-b</module>
    <module>my-project-shared-solution</module>
</modules>

my-project-shared-solution contains the common shared logic for both my-project-solution-a and my-project-solution-b

my-project-solution-a:

<dependencies>
    <dependency>
        <groupId>com.my.solution</groupId>
        <artifactId>my-project-shared-solution</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
</dependencies>

my-project-solution-b:

<dependencies>
    <dependency>
        <groupId>com.my.solution</groupId>
        <artifactId>my-project-shared-solution</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
    </dependency>
</dependencies>

During the build, I want to copy all classes from my-project-shared-solution into my-project-solution-a jar and my-project-solution-b jar.

BUT I don't want to copy the classes from other dependencies like spring-webmvc or jersey-server.

So if my other projects want to include a or b solution, it would still require adding spring-webmvc or jersey-server dependencies, but it would not be needed to add the shared dependency also, because the classes from the shared one will be packed into a and b jars.

I tried something with maven-dependency-plugin but I don't know how to set this up.

Can you provide some examples of how I could do this during the maven build?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.my.solution</groupId>
                        <artifactId>my-project-shared-solution</artifactId>
                        <version>1.0.0</version>
                        <type>jar</type>
                        <classifier>jar-with-dependencies</classifier>
                        <overWrite>true</overWrite>
                        <outputDirectory>libs</outputDirectory>
                        <destFileName>somename.jar</destFileName>
                    </artifactItem>
                </artifactItems>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeTransitive>true</excludeTransitive>
            </configuration>
        </execution>
    </executions>
</plugin>
victorio
  • 6,224
  • 24
  • 77
  • 113
  • If someone uses your project as dependency, they will automatically get all dependencies of your project as well (transitive dependency resolution). So there is no need to copy classes around. – J Fabian Meier Mar 13 '23 at 17:29
  • True, but I don't want to deploy the shared library, because it is useless for anybody. It is useful only for A and B projects. So if someone checks the maven repo, only A and B dependencies should be visible for them. – victorio Mar 13 '23 at 17:44
  • 1
    While I don't think this is a good idea, you can probably achieve this with the Maven shade plugin by just shading in one library. – J Fabian Meier Mar 13 '23 at 19:42
  • 1
    @victorio, Your question keyword should be `create fat jar`. – life888888 Mar 17 '23 at 14:42

0 Answers0