在maven-war-plugin

时间:2015-12-04 07:34:56

标签: java maven war maven-war-plugin

在我们的Web项目中,有几个maven-war-plugin的执行,它们为每个环境生成war文件。我需要为其中一个添加新的依赖项。到目前为止,我们一直在使用叠加层进行此操作,但是这种方法不依赖于drpendencies,因此我们经常最终在不同版本中添加两次相同的库。 有没有办法在每个构建中创建多个战争(不是具有不同配置文件的多个构建)和自定义依赖项?

1 个答案:

答案 0 :(得分:1)

Hy Tomasz W

我已经创建了一个示例pom.xml文件

单独使用maven-war-plugin,您无法仅为一次执行添加依赖项。但是在我的工作示例中,我将war插件输出分离到单独的文件夹中,然后使用依赖插件我复制了仅需要" dev"战争到特定的WEB-INF / lib文件夹...

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.stefanheimberg.stackoverflow-34083152</groupId>
    <artifactId>stackoverflow-34083152</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <!-- maven default properties -->
        <encoding>UTF-8</encoding>
        <maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
        <maven.compiler.showWarnings>true</maven.compiler.showWarnings>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.5.Final</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-dev-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeGroupIds>org.eclipse.persistence</excludeGroupIds>
                            <excludeArtifactIds>eclipselink</excludeArtifactIds>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}-dev/WEB-INF/lib</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeGroupIds>org.hibernate</excludeGroupIds>
                            <excludeArtifactIds>hibernate-core</excludeArtifactIds>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}-test/WEB-INF/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <id>create-dev-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                        <configuration>
                            <warName>${project.build.finalName}-dev</warName>
                            <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>create-test-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                        <configuration>
                            <warName>${project.build.finalName}-test</warName>
                            <webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory>
                        </configuration>
                    </execution>
                </executions>
                <configuration>

                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

文件输出:

$ ls -l target/*/WEB-INF/lib/*
-rw-r--r-- 1 heimbergs 1049089  434678 Okt  1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089  445288 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/antlr-2.7.7.jar
-rw-r--r-- 1 heimbergs 1049089  434678 Okt  1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089  313898 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/dom4j-1.6.1.jar
-rw-r--r-- 1 heimbergs 1049089   75288 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-commons-annotations-5.0.1.Final.jar
-rw-r--r-- 1 heimbergs 1049089 5570751 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-core-5.0.5.Final.jar
-rw-r--r-- 1 heimbergs 1049089  113371 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089  187752 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/jandex-2.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089  714194 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/javassist-3.18.1-GA.jar
-rw-r--r-- 1 heimbergs 1049089   85147 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/javax.json-1.0.4.jar
-rw-r--r-- 1 heimbergs 1049089   66802 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/jboss-logging-3.3.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089   63777 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/validation-api-1.1.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089  109318 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/xml-apis-1.0.b2.jar
-rw-r--r-- 1 heimbergs 1049089  445288 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/antlr-2.7.7.jar
-rw-r--r-- 1 heimbergs 1049089   21007 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/commonj.sdo-2.1.1.jar
-rw-r--r-- 1 heimbergs 1049089  434678 Okt  1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089  313898 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/dom4j-1.6.1.jar
-rw-r--r-- 1 heimbergs 1049089 9051263 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/eclipselink-2.6.1.jar
-rw-r--r-- 1 heimbergs 1049089  187752 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/jandex-2.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089  714194 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javassist-3.18.1-GA.jar
-rw-r--r-- 1 heimbergs 1049089   85147 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javax.json-1.0.4.jar
-rw-r--r-- 1 heimbergs 1049089  162126 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javax.persistence-2.1.0.jar
-rw-r--r-- 1 heimbergs 1049089   66802 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/jboss-logging-3.3.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089   63777 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/validation-api-1.1.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089  109318 Dez  4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/xml-apis-1.0.b2.jar

你看到dev war包含hibernate jar包含传递依赖,而测试war包含eclipselink jar包含。传递依赖。

我认为这不是maven的好用法。但它应该工作。并且它不可扩展..然后你必须管理所有不同战争文件的所有排除列表...... pom.xml可能真的很快就乱了......

<强>建议: 也许这是一个好主意,仔细看看应用程序的架构。部署单元应该适用于所有环境。

<强>更新

  • 使用传递依赖性更新了示例......