如何将war文件添加到另一个Java Web应用程序依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31020038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add war file to another java web application dependencies?
提问by Morteza Malvandi
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:
我有网络 Maven 项目。项目的输出是一个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>
...
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>
?
如何添加了war
与Web应用程序来project_b <groupId>org.itsme</groupId>
,<artifactId>spring3-mvc-maven-xml-hello-world</artifactId>
,<packaging>war</packaging>
和<version>1.0-SNAPSHOT</version>
?
采纳答案by Morteza Malvandi
In Maven, adding dependency is just a piece of cake. Take a look on the following pom.xml.
在 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>
Setting the above dependency is just the same as importing the project-banana.jar in project-apple.
设置上面的依赖和在project-apple中导入project-banana.jar是一样的。
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.
现在我有另一个名为 project-orange 的 Maven Web 应用程序项目,其打包类型等于 war。添加上述依赖项链接根本不起作用,因为 Java 不会将 .war 文件视为类路径。
To solve the problem, there are two approaches:
为了解决这个问题,有两种方法:
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.
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.
创建一个 Maven 模块,其中包含带有 jar 包装的 project-orange 类。现在您可以将新的 Maven 模块视为正常的依赖项。
配置 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>
...
After running mvn install, you can find the following archive files in the target folder
运行 mvn install 后,可以在目标文件夹中找到以下存档文件
- project-orange.war
- project-orange-classes.jar
- 项目-orange.war
- 项目-orange-classes.jar
Now you can edit the pom.xml of project-apple for adding the new dependency.
现在您可以编辑 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>
reference: http://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/
参考:http: //eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/
回答by user6452160
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.
第一步:在原项目的pom中(不依赖任何项目)配置maven war插件,使其既能构建war也能构建jar。当我们执行 mvn clean install 时,jar 将上传到 maven 存储库。
My first part of pom looked like this:
我的 pom 的第一部分是这样的:
<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.
第二步:在第二个项目(依赖于第一个项目)中,将依赖jar(原始项目)放入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
第 3 步:为项目执行 mvn clean install,然后为第二个项目执行
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.:
第 4 步:可选:我们可以创建一个父 pom,而不是像第 3 步中提到的那样按顺序和单独构建项目。我们可以简单地运行 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>