简体   繁体   中英

Maven - generate war and fat jar in same module

I need generate war and fat jar (jar with all dependencies) in same pom.
I found many similar threads on this site, but still have a problem.

In my pom I set packaging to war and add the maven-war-plugin and maven-assembly-plugin :

...
<packaging>war</packaging>
....
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
      <execution>
        <id>make-my-jar-with-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  .... 

Note used maven-assembly-plugin version - 2.2-beta-5 . In my tests its the only version that produce the correct jar file in this configuration. Unfortunately, it also prints many "[INFO] already added, skipping" lines and the build process takes too much time.

If I use the latest version of maven-assembly-plugin (2.6), there are no "already added" info prints and the build at least 3 times faster, but unlike in 2.2 version all my class files placed into /WEB-INF/classes/ folder (should be in root), so I cannon run any main class from this jar. All classes from dependency jars placed in root as expected.

Are there any plugin configuration parameters in the latest version that can help produce the correct jar?

You don't have to get all the classes from dependencies in your root location. Let the assembly plugin do its wonders. As far as running the main class is concerned, you can use the below code in addition to what you already have.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>    // specify your main class here.
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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