简体   繁体   English

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

[英]maven - how to compile code to jar file and not .class

I am trying to build a groovy project using maven. 我正在尝试使用Maven构建一个常规项目。 My packaging type is war file. 我的包装类型是战争档案。 Maven is building the project and putting all dependent libraries in WEB-INF/lib folder but it is compiling all code into class files and putting it into WEB-INF/classes folder. Maven正在构建项目,并将所有依赖库放在WEB-INF / lib文件夹中,但它正在将所有代码编译到类文件中,然后将其放入WEB-INF / classes文件夹中。 Is there a way I can tell maven to build jar file for my project also and put it into WEB-INF/lib folder. 有没有一种方法可以告诉Maven也为我的项目构建jar文件并将其放入WEB-INF / lib文件夹。

My pom.xml looks like this : 我的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>

In these scenarios the usual approach is to you separate your library code in a different module that will be a dependency from your war module. 在这些情况下,通常的方法是将库代码分离到另一个模块中,该模块将独立于war模块。 For this suggestion you can see also how to generate jar and war both in project . 对于此建议,您还可以看到如何在项目中同时生成jar和war

However if you still prefer to go with the solution you mention, you can do it with the following configuration in your pom 但是,如果您仍然喜欢使用您提到的解决方案,则可以在pom中使用以下配置来完成此操作

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

(see http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html , and how to use class file from another war ) (请参阅http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html ,以及如何使用其他战争中的类文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM