【问题标题】:maven - how to compile code to jar file and not .classmaven - 如何将代码编译为 jar 文件而不是 .class
【发布时间】:2014-12-27 15:01:05
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: java maven groovy maven-jar-plugin


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2014-05-28
      • 2011-06-13
      • 2015-11-09
      • 2019-04-07
      • 1970-01-01
      • 2014-03-31
      • 2018-04-01
      • 1970-01-01
      相关资源
      最近更新 更多