Maven项目构建成jar和战争

Maven项目构建成jar和战争

问题描述:

我有一个包含以下 pom.xml 的项目,我从中删除了一些不需要的东西。我打算发生的是生成一个jar文件并最终生成一个 war 包,其中包含 jar 文件图书馆。不幸的是,以下 pom.xml 似乎没有发生这种情况。

I have a project with the following pom.xml and I have removed some of the unwanted stuff from it. What I intended to happen was to generate a jar file and ultimately generate a war package that includes the jar file as a library. Unfortunately, this doesn't seem to be happening with the following pom.xml.

<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>
    <artifactId>test</artifactId>
    <groupId>com.test.abcd</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Test Application</name>

    ....

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <warSourceDirectory>target/www/</warSourceDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>build-war-in-classes</id>
                    <phase>package</phase>
                    <goals>
                        <goal>war</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

</project>

即使这会产生 jar 和在包裹阶段, war war 不包括 jar 文件( WEB-INF / lib / test-0.0.1-SNAPSHOT.jar )作为库,而不是我看到以下类,

Even though this generates both a jar and a war during the package phase, the war doesn't include jar file (WEB-INF/lib/test-0.0.1-SNAPSHOT.jar) as a library and in stead I see the following classes,

META-INF/
META-INF/MANIFEST.MF
....
WEB-INF/
WEB-INF/classes/
....

什么可以导致这种行为?

What could be causing this behaviour ?

我认为你在war插件配置中缺少conf标志:

I think you're missing a conf flag in war plugin configuration:

        <configuration>
          <archiveClasses>true</archiveClasses>
        </configuration>

引自 https://maven.apache.org/plugins/maven-war-plugin/faq.html


如何在我的webapp中创建包含类的JAR?

How do I create a JAR containing the classes in my webapp?

如果您只是想将类和资源打包为WEB-INF / lib中的JAR而不是WEB-INF / classes下的松散文件,
使用以下配置:

If you would simply like to package the classes and resources as a JAR in WEB-INF/lib rather than as loose files under WEB-INF/classes, use the following configuration:

          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <archiveClasses>true</archiveClasses>
            </configuration>
          </plugin>