Как включить зависимость с «предоставленной» областью с плагином maven-assembly-plugin - PullRequest
21 голосов
/ 17 ноября 2011

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

Я использую 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-Независимый для включения зависимых библиотек в файл tar. и, конечно же, я хочу также включить библиотеку log4j.

ниже помпа, унаследованная от супер-родителя:

<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-plug отказывается включать log4j в файл tar? PS, попытка изменить область для компиляции не сработала. Я не могу изменить объявление в супер родительском пом.

Ответы [ 5 ]

12 голосов
/ 31 января 2013

Это можно сделать с помощью плагина 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, который будет включать в себя все скомпилированные и предоставленные ресурсы в разобранном виде, чтобы на них можно было ссылаться в classpath.

java -cp yourproject-bin.jar com.yourcompany.Main
10 голосов
/ 29 ноября 2011

невозможно переопределить «предоставленную» область в maven.

Чтобы решить эту проблему, я объявил переменную в родительском pom, которая будет определять область действия артефакта. Чтобы переопределить область действия, единственное, что нужно сделать, - это установить новое значение для переменной в унаследованном 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>
4 голосов
/ 08 мая 2013

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

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

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

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

0 голосов
/ 26 декабря 2012

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

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

Вы можете просто определить область действия в вашем дескрипторе сборки .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...