Zuned Ahmed
Zuned Ahmed

Reputation: 1387

how to generate jar and war both in project

we have two different project . And and the controller is almost same in two project.

project A has controller inside it. So generating war for it is not problem.

But project B is requiring Controller's jar of project A controller.

Can any body tell me how can i generate the jar file from the controller of project A which i can use in project B?

Upvotes: 5

Views: 25527

Answers (3)

Alexander
Alexander

Reputation: 3035

As @Shashank pointed out, you can make maven also execute the maven-jar-plugin. But this will only create a JAR, that contains the compiled classes only, but not the webapp sources.

There's easy way how to do it in a multi-module project with just a simple mvn clean install command without explicitly invoking mvn clean install jar:jar:

Just add the maven-jar-plugin to the >build>plugins and call it in the "package" phase:

<project>
  <packaging>war</packaging>
  <!-- .. -->
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <classifier>classes</classifier>
        </configuration>
        <executions>
          <execution>
            <id>package-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

This will create a big .war and a smaller -classes.jar artifact.

Update 2017-06-15: If on the other hand, you need an exact copy of the war file (not only the packaged java classes), which is kind of masked as a JAR file, you can use a combination of the maven-dependency-plugin and the maven-install-plugin to create a JAR copy of a WAR. (I happened to be in need of such a solution - don't ask why ;) )

Create a new module that is dependend on the war module:

<project>
  <!-- ... -->
  <dependencies>
    <!-- ... -->
    <dependency>
      <groupId>your-group-id-here</groupId>
      <artifactId>your-war-artifact-id-here</artifactId>
      <version>${project.version}</version>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>your-group-id-here</groupId>
              <artifactId>your-war-artifact-id-here</artifactId>
              <version>${project.version}</version>
              <type>war</type>
              <overWrite>true</overWrite>
              <destFileName>jar-copy-of-war.jar</destFileName>
            </artifactItem>
          </artifactItems>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>true</overWriteSnapshots>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <executions>
          <execution>
            <id>install-copy-of-war</id>
            <phase>install</phase>
            <goals>
              <goal>install-file</goal>
            </goals>
            <configuration>
              <packaging>jar</packaging>
              <classifier>copy-of-war</classifier>
              <artifactId>${project.artifactId}</artifactId>
              <groupId>your-group-id-here</groupId>
              <version>${project.version}</version>
              <file>${project.build.directory}/dependency/jar-copy-of-war.jar</file>
              <generatePom>true</generatePom>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

This will create a copy of the war, name it "--copy-of-war.jar", whereas "copy-of-war" is the classifier for this module.

Upvotes: 0

Shashank Raghunath
Shashank Raghunath

Reputation: 149

Keep project A's packaging as war. Add maven jar plugin to the pom.xml of project A. Build the project with "mvn clean package jar:jar". It will create both a jar and war.

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97409

The default solution for such thing is to have two separate modules one for the war and one for the controller module. Don't start fighting with Maven.

Upvotes: 13

Related Questions