Java 从 pom.xml 为现有 Maven 项目创建 WAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21173514/
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
Create WAR file from pom.xml for an existing Maven project
提问by locorecto
I am creating a web project and I was told that it has to reside inside the resources directory of an existing maven project
我正在创建一个 web 项目,我被告知它必须位于现有 maven 项目的资源目录中
Here is the structure of the project
这是项目的结构
MavenProject
|-- src
| |-- main
| `-- resources
| `-- My-Web-Project
| |-- META-INF
| | `-- MANIFEST.MF
| |-- src
| | |-- classes
| | | |-- com
| | | | `-- example
| | | | `-- projects
| | | | `-- SampleAction.class
| `-- web
| |-- css
| |-- css
| |-- img
| |-- js
| |-- WEB-INF
| | `-- web.xml
| |-- index.jsp
| `-- secondary.jsp
|-- test
`-- pom.xml
As you can see there is already a pom.xml file for the MavenProject. I want to be able to deploy my web project WAR using the current pom.xml.
如您所见,MavenProject 已经有一个 pom.xml 文件。我希望能够使用当前的 pom.xml 部署我的 Web 项目 WAR。
I want to know if it's possible to do this and how would I proceed to create the Maven Plugin.
我想知道是否有可能做到这一点以及我将如何继续创建 Maven 插件。
I read this article but I don't know if it apply to my situation http://maven.apache.org/plugins/maven-war-plugin/usage.html
我读了这篇文章,但我不知道它是否适用于我的情况 http://maven.apache.org/plugins/maven-war-plugin/usage.html
EDIT:
编辑:
I am using tomcat app server.
我正在使用 tomcat 应用服务器。
As Mentioned before, My project "My-Web-Project" should be under "resources"
如前所述,我的项目“My-Web-Project”应该在“resources”下
采纳答案by MariuszS
Read this document, incomplete, but it is good starter.
阅读这个文档,不完整,但它是一个很好的入门。
Using Maven When You Can't Use the Conventions
无法使用约定时使用 Maven
Sample configuration (not recommended)
示例配置(不推荐)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/resources/My-Web-Project/web</directory>
</resource>
</webResources>
</configuration>
</plugin>
This is not complete, target
and classes shouldn't be located inside source directory.
这并不完整,target
类不应位于源目录中。
Maven convention - recommended solution
Maven 约定 - 推荐的解决方案
I was told that it (web project) has to reside inside the resources directory of an existing maven project
我被告知它(网络项目)必须驻留在现有 Maven 项目的资源目录中
This is wrong, web project shouldn't be created inside resources!
这是错误的,不应在资源中创建 Web 项目!
Read about Maven Convention over Configuration.
阅读Maven 约定优于配置。
Convention over configuration is a simple concept. Systems, libraries, and frameworks should assume reasonable defaults. Without requiring unnecessary configuration, systems should "just work".
约定优于配置是一个简单的概念。系统、库和框架应该采用合理的默认值。不需要不必要的配置,系统应该“正常工作”。
Use convention instead of configuration.
使用约定而不是配置。
Proper directory structure should looks like this:
正确的目录结构应如下所示:
MavenProject
|-- src
| |-- main
| | `-- java
| | `-- com
| | `-- example
| | `-- projects
| | `-- SampleAction.java
| `-- resources
| | `-- META-INF
| | `-- MANIFEST.MF
| `-- webapp
| |-- css
| |-- img
| |-- js
| |-- WEB-INF
| | `-- web.xml
| |-- index.jsp
| `-- secondary.jsp
|-- test
|-- target // this is generated by maven!
| |-- classes
| | `-- com
| | `-- example
| | `-- projects
| | `-- SampleAction.class
`-- pom.xml
Source: Introduction to the Standard Directory Layout
来源:标准目录布局简介
回答by gerrytan
What app server are you using? Most app servers already have its own maven plugin capable of deploying war so you don't have to write yourself: jboss-maven-plugin, tomcat-maven-plugin.
您使用的是什么应用服务器?大多数应用服务器已经有自己的能够部署 war 的 maven 插件,所以你不必自己编写:jboss-maven-plugin,tomcat-maven-plugin。
Also your folder structure doesn't comply with maven standards. Typically if you follow the standards and set your archive type into war, on package goal maven will automatically generate a war for you in the target folder.
此外,您的文件夹结构不符合maven 标准。通常,如果您遵循标准并将存档类型设置为 war,则在 package 目标上 maven 会自动在目标文件夹中为您生成一个 war。