Подтвердить что ты не робот

Как включить зависимость с "предоставленной" областью с помощью maven-assembly-plugin

Я борюсь с maven, чтобы включить управляемую зависимость с областью "предоставлено" в файл tar, используя плагин maven-assembly-plugin.

Я использую супер родительский файл pom в качестве базы для всех моих проектов. большинство проектов будут развернуты под сервером приложений, поэтому две основные зависимости объявляются под супер родительским pom. ниже это соответствующий раздел управления от суперпользователя:

http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx.integration</groupId>
    <artifactId>super-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.1.3</version>
    <name>super parent</name>
    <url>http://maven.apache.org.check</url>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

log4j.version = 2.0.8

в одном из унаследованных проектов (который является автономным приложением), я использую maven-assembly-plugin с dependencySets, чтобы включить зависимые библиотеки в tar файл. и, конечно, я хочу также включить библиотеку log4j.

ниже - это 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.xxx.integration</groupId>
        <artifactId>super-parent</artifactId>
        <version>1.1.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>plugin-cc-checker</artifactId>
    <name>plugin-cc-checker</name>
    <version>2.1</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>com.orca.integration</groupId>
                        <artifactId>integration-assembly-descriptor</artifactId>
                        <version>1.1.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>make-assembly-according-to-distribution-xml</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${artifactId}</finalName>
                            <!-- This is where we use our shared assembly descriptor -->
                            <descriptors>
                                <descriptor>distribution-app.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xerces</artifactId>
            <version>${xerces.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>${commons-logging-api.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>excalibur</groupId>
            <artifactId>excalibur-i18n</artifactId>
            <version>${excalibur-i18n.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.snmp4j</groupId>
            <artifactId>snmp4j</artifactId>
            <version>${snmp4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

файл distribution-app.xml:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- Add module dependencies and the jar that is created in the packaging 
        phase. Product name will be <project name>-app-<version no>.tar -->
    <id>app-${version}</id>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>resources/app</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <excludes>
                <!-- Since there is a bug in xalan 2.7.1 all applications required to 
                    use xalan-orca jar file -->
                <exclude>xalan:xalan</exclude>
            </excludes>
            <!-- includes> <include>*</include> </includes-->
        </dependencySet>
    </dependencySets>
    <moduleSets>
        <moduleSet>
            <binaries>
                <outputDirectory>/guy</outputDirectory>
                <includes>
                    <include>log4j:log4j</include>
                </includes>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

Почему maven-assembly-plugin отказывается включать log4j в tar файл? PS, попытка сменить область компиляции не помогла. Я не могу изменить объявление в супер родительском pom.

4b9b3361

Ответ 1

Это можно сделать с помощью плагина Assembly.

Сначала создайте assembly.xml со следующим:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
        <dependencySet>
            <unpack>true</unpack>
            <scope>provided</scope>
        </dependencySet>
    </dependencySets>
</assembly>

Затем просто включите его в pom.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptor>src/main/assembly/assembly.xml</descriptor>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Это создаст файл yourproject-bin.jar, который будет включать все компилируемые и предоставленные ресурсы взорваться, чтобы они могли ссылаться в пути к классам.

java -cp yourproject-bin.jar com.yourcompany.Main

Ответ 2

невозможно переопределить область "предоставлено" в maven.

Чтобы решить эту проблему, я объявила переменную родительского пом, которая определит область действия артефакта. Чтобы переопределить область действия, единственное, что нужно сделать, - установить новое значение для переменной в унаследованной pom.

См. пример ниже:

parent pom:

    <properties>
            <log4j.version>1.2.8</log4j.version>
            <log4j.scope>provided</log4j.scope>
    </properties>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>${log4j.scope}</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Теперь в дочернем pom просто объявите переменную снова:

<properties>
        <log4j.scope>runtime</log4j.scope>
</properties>

Ответ 3

Вы можете переопределить управляемую зависимость, объявив ее внутри тега <dependencyManagement> в POM, где вы хотите, чтобы она была переопределена.

В вашем случае вы должны добавить следующее вашему ребенку pom:

<dependencyManagement>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencyManagement>

Обратите внимание, что это переопределяет все, что указано в родительском управлении зависимостями POM, то есть вы не можете оставить version или scope необъявленным и ожидать, что он будет унаследован.

Ответ 5

Я столкнулся с подобной проблемой, пытаясь собрать проект с зависимостью с предоставленной областью. Я нашел обходное решение для этой проблемы:

  • Оставьте зависимость в области "предоставлено"
  • Скопируйте зависимость в целевую папку с помощью модуля maven-dependency-plugin (example)
  • Используйте файловый набор в дескрипторе сборки, чтобы связать зависимость как файл.