简体   繁体   English

从pom.xml为现有Maven项目创建WAR文件

[英]Create WAR file from pom.xml for an existing Maven project

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. 如您所见,MavenProject已有一个pom.xml文件。 I want to be able to deploy my web project WAR using the current 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 app server。

As Mentioned before, My project "My-Web-Project" should be under "resources" 如前所述,我的项目“My-Web-Project”应该在“资源”之下

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 我被告知它(web项目)必须驻留在现有maven项目的资源目录中

This is wrong, web project shouldn't be created inside resources! 这是错误的,不应该在资源内部创建Web项目!

Read about Maven Convention over Configuration . 阅读Maven Convention over Configuration

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 来源: 标准目录布局简介

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 . 大多数应用服务器已经拥有自己的maven插件,可以部署战争,所以你不必自己编写: jboss-maven-plugintomcat-maven-plugin

Also your folder structure doesn't comply with maven standards . 您的文件夹结构也不符合maven标准 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. 通常,如果您遵循标准并将存档类型设置为war,则在包目标maven将自动在目标文件夹中为您生成战争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM