maven - 如何将代码编译为jar文件而不是.class

时间:2014-10-31 10:14:10

标签: java maven groovy maven-jar-plugin

我正在尝试使用maven构建一个groovy项目。我的包装类型是war文件。 Maven正在构建项目并将所有依赖库放在WEB-INF / lib文件夹中,但是它将所有代码编译成类文件并将其放入WEB-INF / classes文件夹中。有没有办法告诉maven为我的项目构建jar文件并将其放入WEB-INF / lib文件夹。

我的pom.xml看起来像这样:

<groupId>com.myproject</groupId>
<artifactId>ExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Example Project</name>
<url>http://maven.apache.org</url>
<dependencies>

...
...
...
</dependencies>
<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/groovy</directory>
            <excludes>
                <exclude>**/*.groovy</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/groovy</source>
                            <source>src/main/resources</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                            <source>src/test/resources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>master</finalName>
</build>

1 个答案:

答案 0 :(得分:1)

在这些场景中,通常的方法是将库代码分离到另一个模块中,该模块将是war模块的依赖项。对于此建议,您还可以看到how to generate jar and war both in project

但是,如果你仍然愿意使用你提到的解决方案,你可以在你的pom中使用以下配置来实现它

<configuration>
  ..
  <attachClasses>true</attachClasses>
  <archiveClasses>true</archiveClasses>
</configuration>

(请参阅http://maven.apache.org/plugins/maven-war-plugin/war-mojo.htmlhow to use class file from another war