如何将war文件添加到另一个java Web应用程序依赖项? [英] How can I add war file to another java web application dependencies?
问题描述
我有web maven项目。 Project的输出是一个 war
文件。我想将此 war
文件添加为project_b的依赖项。
project_b的 pom.xml
文件,有一个插件如下:
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>
...
如何添加战争使用
< groupId> org.itsme< / groupId>
,< artifactId> spring3-mvc-的project_b的web应用程序code> maven-xml-hello-world< / artifactId>
,< packaging> war< / packaging>
和<版本> 1.0-SNAPSHOT< / version>
?
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>
?
推荐答案
在Maven中,添加依赖只是一个小菜一碟。看看下面的pom.xml。
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>
设置上述依赖项与在project-apple中导入project-banana.jar相同。
Setting the above dependency is just the same as importing the project-banana.jar in project-apple.
现在我有另一个名为project-orange的Maven Web应用程序项目,其打包类型等于war。添加上述依赖关系链接根本不起作用,因为Java没有将.war文件视为类路径。
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.
要解决此问题,有两种方法:
-
创建一个Maven模块,其中包含带有jar包装的项目橙色类。现在,您可以将新的Maven模块视为正常依赖项。
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.
配置maven-war-plugin,以便在构建时构建.jar文件。战争档案。在war项目的节点下添加以下代码。以下是一个例子。
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>
...
运行mvn install后,您可以在中找到以下存档文件目标文件夹
After running mvn install, you can find the following archive files in the target folder
- project-orange.war
- project-orange-classes.jar
现在你可以编辑project-apple的pom.xml来添加新的依赖项。
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>
参考: http://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar -package /
这篇关于如何将war文件添加到另一个java Web应用程序依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!