how to use class file from another war

16,966

Solution 1

You can define the war plugin to produce a separate jar file which is available via a classifier based on the configuration:

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

After that you can use this as a separate dependency in other projects. But the best to make a separate jar module out of it.

Solution 2

What you are doing is using a WAR file overlay. Maven does support this. However...

The WAR plugin executes in the package phase. Any plugin, such as the Java compiler (which runs in the compile phase), that executes before this phase will not see any files that the WAR plugin has extracted. (here is the reference list of all Maven phases if that helps)

If you have the ability to refactor your project structure, the best way is to create a normal JAR project with your controllers and have both WARs pull this JAR in as a dependency.

If there is some reason why you can't do this, you will need to do something like:

  1. Configure the WAR plugin to run in a phase earlier than compile, such as generate-resources and makes sure it extracts the overlay class files to ${project.build.outputDirectory}.

  2. You could also use the Maven Dependency Plugin's unpack goal to extract classes from the dependency WAR to ${project.build.outputDirectory}.

But I do recommend if at all possible to refactor your controllers into a separate JAR, it is much easier and more maintainable.

Solution 3

put the controllers into a new jar project and make dependencies from your webprojects to this controllers.

Solution 4

Add the following plugins to your share maven war module (for creating war and jar artifact at the same time):

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>make-a-jar</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>       
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <packaging>jar</packaging>
                        <artifactId>${project.artifactId}</artifactId>
                        <groupId>${project.groupId}</groupId>
                        <version>${project.version}</version>
                        <file>
                            ${project.build.directory}/${project.artifactId}-${project.version}.jar
                        </file>
                    </configuration>
                </execution>
            </executions>
        </plugin> 

then add dependency to your dependent maven war module.

Solution 5

Java EE visability standards state that classes from one war shouldn't be available to classes in another war, when they're packaged as an EAR. Most containers will enforce this strictly.

You should put the code you wish to share into a JAR and include that as a dependency in the war you want to use.

Share:
16,966
Zuned Ahmed
Author by

Zuned Ahmed

Updated on June 05, 2022

Comments

  • Zuned Ahmed
    Zuned Ahmed almost 2 years

    We have two different web application and we want to extend few controllers of one war into another war.

    We are using maven to build the project.

    to include war we have given its dependency as

    <dependency>
                <groupId>com.abc.exchange</groupId>
                <artifactId>employer</artifactId>
                <version>2.3.M2-SNAPSHOT</version>
                <type>war</type>
                <scope>compile</scope>
            </dependency>
    

    It is unable to build giving class not found exception.

    Can any body help me out how to achieve this?

    I am getting error maven build failed :

    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project agent-war: Compilation failure: Compilation failure:
    [ERROR] \projects\trunk_new\agent\src\main\java\com\platform\agent\web\AgentEmployeeController.java:[22,41] cannot find symbol
    [ERROR] symbol  : class EmployeeController
    [ERROR] location: package com..platform.employer.web
    
  • Samuel EUSTACHI
    Samuel EUSTACHI about 12 years
    I agree, this is the right approach. And very easy thanks to maven
  • Zuned Ahmed
    Zuned Ahmed about 12 years
    how can i refract-or my controller into separate jar file? any link for the same.
  • Stephane
    Stephane over 8 years
    I reckoned this would be fine for a project that only needs a war for its acceptance tests run against the war archive.
  • Abhishek Chatterjee
    Abhishek Chatterjee over 7 years
  • Kuronashi
    Kuronashi over 3 years
    to use the jar add <classifier>classes</classifier> to the dependency reference