Изменить зависимость maven для артефакта с помощью классификатора

С помощью плагина maven jar я создаю два jar: bar-1.0.0.jar и bar-1.0.0-client.jar.

На самом деле в моем POM у меня есть следующая зависимость:

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

Этот артефакт существует также в двух версиях bar-1.0.0.jar и bar-1.0.0-client.jar.

Я хочу сделать bar-1.0.0-client.jar зависимым от foo-1.0.0-client.jar и bar-1.0.0.jar зависимым от foo-1.0.0.jar.

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

-> Первое (неправильное) решение: определите предоставленную область и используйте правильный пакет foo при использовании bar.jar

-> Второе (длинное) решение: добавить классификатор «сервер» в другую банку. Используйте другой профиль, чтобы создать артефакт foo и поместить классификатор в свойство.

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

================
Относительно решения профиля.

Модуль интерфейсов пом

<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>

Модуль реализации 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>

Проблема с этим решением связана с тем, что в моем клиентском интерфейсе отсутствуют некоторые интерфейсы, и maven выдает ошибку компиляции на этапе компиляции.

И если я использую myapp-model и другой проект, у меня не будет зависимости от правильного интерфейса myapp.

Интересно, можно ли построить банку и поместить внутрь конкретный помпон?


person Vlagorce    schedule 02.12.2010    source источник
comment
Длинное решение правильное. Так в чем вопрос?   -  person Sean Patrick Floyd    schedule 02.12.2010
comment
На самом деле это не сработало. Поскольку в моих foo-server и foo-client у меня не было удаленных интерфейсов. Когда я создаю проект, я сталкиваюсь с ошибкой компиляции. Я отредактирую свой вопрос, чтобы объяснить эту проблему.   -  person Vlagorce    schedule 03.12.2010
comment
Старый вопрос по той же проблеме [1]: почтовый архив .com / users @ maven.apache.org / msg102761.html   -  person Vlagorce    schedule 03.12.2010
comment
Почему отсутствуют интерфейсы? Как раньше работало с отсутствующими интерфейсами?   -  person Mike Cornell    schedule 06.12.2010
comment
По умолчанию maven (m2eclipse) использует model.jar без классификатора, поэтому сначала все компилируется. Но когда maven повторно запускает упаковку для каждого профиля, он пытается снова скомпилировать. Отсутствующие интерфейсы = ›Я не хочу делиться интерфейсами на стороне сервера с клиентской стороной. После поиска в Google / разговора / тестирования нет никакого решения. Если я действительно не хочу делиться интерфейсами, мне нужно два модуля maven.   -  person Vlagorce    schedule 07.12.2010


Ответы (1)


Для интерфейсов.

Я ничего не меняю и создаю оба interfaces.jar (клиент + сервер).

Для модели я импортирую обе банки по желанию.

<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>

Благодаря этому я могу построить обе версии модели без каких-либо ошибок.

В моем клиентском и серверном приложениях

Для каждого приложения я создаю зависимость от правильных interfaces.jar и models.jar

person Vlagorce    schedule 07.12.2010
comment
Думаю, вы перепутали классификаторы - один должен быть client, другой - server. - person carlspring; 01.03.2012