如何将war文件添加到另一个java Web应用程序依赖项?

Mor*_*ndi 19 java web-applications maven-plugin

我有web maven项目.Project的输出是一个war文件.我想将此war文件添加为project_b的依赖项.
project_b的pom.xml文件有一个插件如下:

...
<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>
...
Run Code Online (Sandbox Code Playgroud)

如何添加的warWeb应用程序与到project_b <groupId>org.itsme</groupId>,<artifactId>spring3-mvc-maven-xml-hello-world</artifactId>,<packaging>war</packaging><version>1.0-SNAPSHOT</version>

Mor*_*ndi 38

在Maven中,添加依赖只是一块蛋糕.看看下面的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>
Run Code Online (Sandbox Code Playgroud)

设置上述依赖项与在project-apple中导入project-banana.jar相同.

现在我有另一个名为project-orange的Maven Web应用程序项目,其打包类型等于war.添加上述依赖关系链接根本不起作用,因为Java没有将.war文件视为类路径.

要解决这个问题,有两种方法:

  1. 创建一个Maven模块,其中包含带有jar包装的项目橙色类.现在,您可以将新的Maven模块视为正常依赖项.

  2. 配置maven-war-plugin,以便在构建.war文件时构建.jar文件.在war项目的节点下添加以下代码.以下是一个例子.

.

...
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <attachClasses>true</attachClasses>
                <classesClassifier>classes</classesClassifier>
            </configuration>
        </plugin>
    </plugins>
</build>
...
Run Code Online (Sandbox Code Playgroud)

运行mvn install后,您可以在目标文件夹中找到以下存档文件

  • 项目orange.war
  • 项目 - 橙 - classes.jar

现在,您可以编辑project-apple的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>
        <!-- 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>
Run Code Online (Sandbox Code Playgroud)

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


小智 6

最近,我也面临着同样的问题。我就是这样解决的,非常简单。

\n\n

步骤1:在原始项目的pom中(不依赖任何项目)配置maven war插件,使其既构建war又构建jar。当我们进行 mvn clean install 时,jar 将被上传到 maven 存储库中。

\n\n

我的 pom 的第一部分如下所示:

\n\n
<modelVersion>4.0.0</modelVersion>\n<groupId>com.pancharatna</groupId>\n<artifactId>TestWebApp</artifactId>\n**<packaging>jar</packaging>**\n<version>1.0-SNAPSHOT</version>\n
Run Code Online (Sandbox Code Playgroud)\n\n

接下来,在构建部分添加另一个插件:

\n\n
<build>\n    <finalName>TestWebApp</finalName>\n    <plugins>\n        **<plugin>\n            <groupId>org.apache.maven.plugins</groupId>\n            <artifactId>maven-war-plugin</artifactId>\n            <executions>\n                <execution>\n                    <id>make-a-war</id>\n                    <phase>compile</phase>\n                    <goals>\n                        <goal>war</goal>\n                    </goals>\n                </execution>\n            </executions>\n        </plugin>**\n\n        <plugin>\n            <artifactId>maven-compiler-plugin</artifactId>\n            <configuration>\n                <source>1.6</source>\n                <target>1.6</target>\n            </configuration>\n        </plugin>\n\n    </plugins>\n</build>\n
Run Code Online (Sandbox Code Playgroud)\n\n

步骤2:在第二个项目(依赖于第一个项目)中,将依赖jar(原始项目)放入pom.xml中。

\n\n
   <dependency>\n        <groupId>com.pancharatna</groupId>\n        <artifactId>TestWebApp</artifactId>\n        <version>1.0-SNAPSHOT</version>\n        <scope>compile</scope>\n    </dependency>\n
Run Code Online (Sandbox Code Playgroud)\n\n

步骤 3: 对项目执行 mvn clean install,然后对第二个项目执行 mvn clean install

\n\n

步骤 4:可选:我们可以创建一个父 pom,而不是像步骤 3 中提到的那样按顺序单独构建项目。\n我们可以简单地运行 mvn clean install:

\n\n
C:/Test/\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80Project1\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/main/...\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pom.xml\n        \xe2\x94\x82\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80Project2\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src/main/...\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pom.xml\n        \xe2\x94\x82\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80pom.xml   <!-- parent pom -->\n\n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\n    <modelVersion>4.0.0</modelVersion>\n    <groupId>com.MyApp</groupId>\n    <artifactId>myproject</artifactId>\n    <packaging>pom</packaging>\n    <version>1.0-SNAPSHOT</version>\n    <name>myproject</name>\n    <url>http://maven.apache.org</url>\n    </dependencies>\n    <modules>\n        <module>/Project1</module>\n        <module>/Project2</module>\n    </modules>\n</project>\n
Run Code Online (Sandbox Code Playgroud)\n