5

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?

Zuned Ahmed
  • 1,357
  • 6
  • 29
  • 56
  • 1
    possible duplicate of [maven deploy additional jar file](http://stackoverflow.com/questions/754216/maven-deploy-additional-jar-file) – Danubian Sailor Nov 12 '13 at 08:05

3 Answers3

13

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.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 1
    you mean to say should i write two pom file one for generating jar file and other for war file? – Zuned Ahmed Apr 17 '12 at 11:27
  • The best thing is to create a multi-module build which contains the two modules (war, controller) and a parent. – khmarbaise Apr 17 '12 at 11:29
  • 3
    But you can also use the solution i described here: http://stackoverflow.com/a/10189767/296328 – khmarbaise Apr 17 '12 at 11:30
  • Arguably if you need the jar you should have the war and business logic code separated anyway. That is I assume you want to reuse the code outside of the war... hence the jar. – Adam Gent Apr 17 '12 at 11:42
  • the problem its not the business logic the UI really complex and for that we have done good amount of coding on controller and web model object and that model object is used on different portal so here we have to port complete controllers from one portal to other. – Zuned Ahmed Apr 17 '12 at 15:26
  • OMG! Just use Maven `profiles` and change the packaging to `JAR` and build both profiles! – ingyhere Sep 17 '21 at 19:45
0

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.

0

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.

Alexander
  • 2,925
  • 3
  • 33
  • 36