2

我正在尝试构建一个(战争)项目,在该项目中我从另一段代码中复制并修改了 pom.xml 文件,并且由于某种原因,当我mvn clean package使用该软件时,它没有将类包含到关联的 *-classes.jar 中文件。Maven 版本在 Ubuntu 12.10 上为 2.2.1。

我是 maven 的新手,主要通过示例中的复制和粘贴来管理,所以我基本上不知道我的 pom 是如何工作的(它包含在下面的全部内容中)。

该项目有一个<packaging>war</packaging>元素,并且maven-war-plugin配置部分说archiveClassesand attachClasses,我认为这是导致构建 *-classes.jar 文件的原因。运行构建后,我的目标目录如下所示:

richard@holly:~/Code/External/JavaServer2.0/target$ ls 
apidocs  generated-sources  surefire           sword2-server-1.0-classes.jar  sword2-server-1.0-sources.jar
classes  maven-archiver     sword2-server-1.0  sword2-server-1.0-javadoc.jar  sword2-server-1.0.war

但是 Sword2-server-1.0-classes.jar 不包含任何类:

richard@holly:~/Code/External/JavaServer2.0/target$ jar tf sword2-server-1.0-classes.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/maven/
META-INF/maven/org.swordapp/
META-INF/maven/org.swordapp/sword2-server/
META-INF/maven/org.swordapp/sword2-server/pom.xml
META-INF/maven/org.swordapp/sword2-server/pom.properties

同时该目录下的所有其他 *.jar 文件都包含了源文件的所有相关信息(javadocs、source 等,都是完整的)。

毫无疑问,我错过了某种形式的插件配置,但到目前为止我无法理解 maven 插件文档,因此非常感谢任何帮助。

(几乎)完整的 pom.xml(为了简洁省略了实际的依赖项):

<?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>org.swordapp</groupId>
<artifactId>sword2-server</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>SWORD v2 :: Common Server Library</name>
<description>
    Common Server Library with interfaces to be implemented by servers
    wishing to provide SWORD v2 support
</description>
<url>http://www.swordapp.org/</url>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-release-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>6</source>
                <target>6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>
                            true
                        </addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>
                            true
                        </addDefaultSpecificationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>default</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
                <execution>
                    <id>default</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <attachClasses>true</attachClasses>
                <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                <warSourceExcludes>WEB-INF/lib/*.jar</warSourceExcludes>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>${basedir}/src/main/webapp</directory>
                        <includes>
                            <include>WEB-INF/web.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    ....
</dependencies>
</project>
4

2 回答 2

6

It turns out that archiveClasses and attachClasses don't play well together. The documentation for attachClasses says that it will put the files in the webapps classes directory into a *-classes.jar during the war build. But archiveClasses places the content of the classes directory in the webapp into a jar file in the webapp's lib directory. Since this means that there are no classes in the classes directory to be put into the *-classes.jar if both these configuration flags are set to true.

The answer is to remove archiveClasses from the configuration, and then everything behaves as expected.

This seems like it's probably a maven bug, and it behaves the same in both maven 2 and maven 3.

于 2012-12-19T16:53:10.810 回答
3

您应该尝试通过降低复杂性来归结问题,只需先尝试如下简单配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <archiveClasses>true</archiveClasses>
        <attachClasses>true</attachClasses>
    </configuration>
</plugin>

之后,只需尝试:

mvn clean package
于 2012-12-15T19:15:45.757 回答