Java 如何在 Eclipse 中使用 Maven 构建 WAR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3116605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:36:04  来源:igfitidea点击:

How can I build WAR with Maven in Eclipse?

javamaven-2web-applications

提问by newbie

I have project that I'm now starting as Maven project, but for some reason it is not working. Here is my 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>
    <groupId>ProgramName</groupId>
    <artifactId>ProgramName</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.core</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>

        <!-- all other dependecies here -->

    </dependencies>
    <build>
        <finalName>ProgramName</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <targetPath>WEB-INF</targetPath>
                            <directory>src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>*.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
    </build>
</project>

采纳答案by Pascal Thivent

Actually, your POM looks a bit weird:

实际上,您的 POM 看起来有点奇怪:

  • it is missing the right packagingfor a webapp project.
  • the maven war plugin configuration doesn't look right, you just don't need the extra stuff you added.
  • 它缺少packagingwebapp 项目的权利。
  • maven war 插件配置看起来不正确,您只是不需要添加的额外内容。

Here is what a minimal pom looks like:

这是一个最小的 pom 的样子:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
  </build>
</project>

So either modify it and update the project configuration (right-clickon your project then Maven > Update Project Configuration).

因此,要么修改它并更新项目配置(右键单击您的项目,然后单击Maven > Update Project Configuration)。

Or just start over and create your project using the maven-archetype-webapp. You can do this from Eclipse: New > Project... > Maven Project, then select the maven-archetype-webappin the wizzard and follow the seps.

或者只是重新开始并使用maven-archetype-webapp. 您可以从 Eclipse 执行此操作:New > Project... > Maven Project,然后在向导中选择maven-archetype-webapp并按照 sep 进行操作。

Or from the command line:

或者从命令行:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

回答by zengr

I suggest use m2eclipse. It's a one click operation using the Eclipse plugin.

我建议使用m2eclipse。这是使用 Eclipse 插件的一键式操作。

回答by xor_eq

for instance, I see

例如,我看到

<packaging>war</packaging>

missing in your pom, you should have a look at how the maven-war-pluginis used.

在你的 pom 中缺少,你应该看看maven-war-plugin是如何使用的。