Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change maven dependency for artifact using classifier

With the maven jar plugin I build two jar: bar-1.0.0.jar and bar-1.0.0-client.jar.

Actually in my POM I have the following dependency:

<dependency>   
   <groupId>de.app.test</groupId>
   <artifactId>foo</artifactId>
   <version>1.0.0</version>
</dependency>

This artifact exist also in two version bar-1.0.0.jar and bar-1.0.0-client.jar

I want to make bar-1.0.0-client.jar dependent of foo-1.0.0-client.jar and bar-1.0.0.jar dependent of foo-1.0.0.jar.

================

->First (wrong) solution: define the scope as provided and use the right foo package when using bar.jar

->Second (long) solution : Add 'server' classifier to the other jar. Use different profile to build the foo artifact and put the classifier in a property.

<dependency>
    <groupId>de.app.test</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0</version>
    <classifier>${profile.classifier}<classifier>
</dependency>

================
Concerning the profile solution.

Interfaces module pom

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.app</groupId>
        <artifactId>myapp-parent</artifactId>
        <version>1.1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.app</groupId>
    <artifactId>myapp-interfaces</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>myapp Interfaces</name>
    <profiles>
        <profile>
            <id>server</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jar-server</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>server</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>client</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jar-client</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>client</classifier>
                                    <excludes>
                                        <exclude>**/server/**</exclude>
                                    </excludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Implementation module pom

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.app</groupId>
        <artifactId>myapp-parent</artifactId>
        <version>1.1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.app</groupId>
    <artifactId>myapp-model</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>myapp Model</name>
    <properties>
        <myapp-interfaces.classifier></myapp-interfaces.classifier>
        <myapp-interfaces.version>1.1.0-SNAPSHOT</myapp-interfaces.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>com.app</groupId>
            <artifactId>myapp-interfaces</artifactId>
            <version>${myapp-interfaces.version}</version>
            <classifier>${myapp-interfaces.classifier}</classifier>
        </dependency>
        [...]
    </dependencies>
    <profiles>
        <profile>
            <id>server</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jar-server</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>server</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <dependencies>
                <dependency>
                    <groupId>com.app</groupId>
                    <artifactId>myapp-interfaces</artifactId>
                    <version>${myapp-interfaces.version}</version>
                    <classifier>${myapp-interfaces.classifier}</classifier>
                </dependency>
            </dependencies>
            <properties>
                <myapp-interfaces.classifier>server</myapp-interfaces.classifier>
            </properties>
        </profile>
        <profile>
            <id>client</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jar-client</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>client</classifier>
                                    <excludes>
                                        <exclude>**/server/**</exclude>
                                        <exclude>**/META-INF/services/**</exclude>
                                    </excludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <myapp-interfaces.classifier>client</myapp-interfaces.classifier>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.app</groupId>
                    <artifactId>myapp-interfaces</artifactId>
                    <version>${myapp-interfaces.version}</version>
                    <classifier>${myapp-interfaces.classifier}</classifier>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

The problem with this solution is due to the fact that my client interface have some missing interfaces and maven throw a compilation error during the compile phase.

And if I use myapp-model and an other project I didn't not have dependency to the right myapp-interface.

I wonder if it's possible to build a jar and put a specific pom inside ?

like image 497
Vlagorce Avatar asked Dec 02 '10 14:12

Vlagorce


1 Answers

For the Interfaces.

I change nothing and build the both interfaces.jar (client + server).

For the Model I import the both jar as optional

<dependency>
        <groupId>com.app</groupId>
        <artifactId>myapp-interfaces</artifactId>
        <version>${myapp-interfaces.version}</version>
        <classifier>client</classifier>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.app</groupId>
        <artifactId>myapp-interfaces</artifactId>
        <version>${myapp-interfaces.version}</version>
        <classifier>server</classifier>
        <optional>true</optional>
    </dependency>

With that I can build the both model's version without any error.

In my client application and server application

For each application I create the dependency to the right interfaces.jar and models.jar

like image 72
Vlagorce Avatar answered Oct 24 '22 03:10

Vlagorce