How can I add war file to another java web applica

2020-02-03 11:29发布

I have web maven project. Project's output is a war file. I want to add this war file as dependency to project_b.
The project_b's pom.xml file, have a plugin as following:

...
<plugin>
    <inherited>true</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <warName>geoserver</warName>
        <webappDirectory>${project.build.directory}/geoserver</webappDirectory>
        <packagingExcludes>WEB-INF/lib/servlet-api*.jar</packagingExcludes>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
            <manifestEntries>
                <GeoServerModule>core</GeoServerModule>
                <Application-Name>${project.build.finalname}</Application-Name>
                <Project-Version>${project.version}</Project-Version>
                <Iteration-Name>${iteration}</Iteration-Name>
                <Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
                <Git-Revision>${build.commit.id}</Git-Revision>
            </manifestEntries>
        </archive>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>
...

How do I add the war of web application to project_b with <groupId>org.itsme</groupId>, <artifactId>spring3-mvc-maven-xml-hello-world</artifactId>, <packaging>war</packaging> and <version>1.0-SNAPSHOT</version>?

2条回答
Juvenile、少年°
2楼-- · 2020-02-03 12:16

Recently, I was also facing the same problem. This is how I solved it very simply.

Step 1: In the pom of original project(Not dependent on any project)Configure the maven war plugin in such a way that it will build war as well as jar. The jar will be uploaded into the maven repository when we do mvn clean install.

My first part of pom looked like this:

<modelVersion>4.0.0</modelVersion>
<groupId>com.pancharatna</groupId>
<artifactId>TestWebApp</artifactId>
**<packaging>jar</packaging>**
<version>1.0-SNAPSHOT</version>

Next, add another plugin in the build section:

<build>
    <finalName>TestWebApp</finalName>
    <plugins>
        **<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-a-war</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>war</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>**

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

    </plugins>
</build>

Step 2: In the 2nd project(which is dependent on the first project), put the dependency jar(Original Project)in the pom.

   <dependency>
        <groupId>com.pancharatna</groupId>
        <artifactId>TestWebApp</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>

Step 3: Do mvn clean install for the project and then for the second project

Step 4: Optional : Instead of building the projects sequentially and separately as mentioned in Step 3, we can create a parent pom. We can simple run mvn clean install.:

C:/Test/
        ├───Project1
        │   └───src/main/...
        │   └───pom.xml
        │
        ├───Project2
        │   └───src/main/...
        │   └───pom.xml
        │
        ├───pom.xml   <!-- parent pom -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.MyApp</groupId>
    <artifactId>myproject</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myproject</name>
    <url>http://maven.apache.org</url>
    </dependencies>
    <modules>
        <module>/Project1</module>
        <module>/Project2</module>
    </modules>
</project>
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-03 12:28

In Maven, adding dependency is just a piece of cake. Take a look on the following pom.xml.

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
    <modelVersion>4.0.0</modelVersion>

    <!-- Project Details -->
    <groupId>ykyuen</groupId>
    <artifactId>project-apple</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>project-apple</name>

    <dependencies>
        <!-- project-apple depends on project-banana -->
        <dependency>
            <groupId>ykyuen</groupId>
            <artifactId>project-banana</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

Setting the above dependency is just the same as importing the project-banana.jar in project-apple.

Now i have another Maven web application project called project-orange with packaging type equals to war. Adding the above dependency linkage does not work at all since Java does not see .war file as a classpath.

To solve the problem, there are two approaches:

  1. Create a Maven module which contains the classes of project-orange with jar packaging. Now you can treat the new Maven module as normal dependency.

  2. Configure the maven-war-plugin such that it will build the .jar file when building the .war file. Add the following code under the node of your war project. The following is an example.

.

...
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <attachClasses>true</attachClasses>
                <classesClassifier>classes</classesClassifier>
            </configuration>
        </plugin>
    </plugins>
</build>
...

After running mvn install, you can find the following archive files in the target folder

  • project-orange.war
  • project-orange-classes.jar

Now you can edit the pom.xml of project-apple for adding the new dependency.

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
    <modelVersion>4.0.0</modelVersion>

    <!-- Project Details -->
    <groupId>ykyuen</groupId>
    <artifactId>project-apple</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>project-apple</name>

    <dependencies>
        <!-- project-apple depends on project-banana -->
        <dependency>
            <groupId>ykyuen</groupId>
            <artifactId>project-banana</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- project-apple depends on project-orange -->
        <dependency>
            <groupId>ykyuen</groupId>
            <artifactId>project-orange</artifactId>
            <version>1.0</version>
            <!-- To map the project-orange-classes.jar -->
            <classifier>classes</classifier>
        </dependency>
    </dependencies>
</project>

reference: http://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/

查看更多
登录 后发表回答