如何在 Eclipse 中使用 Maven 构建 WAR? [英] How can I build WAR with Maven in Eclipse?

查看:27
本文介绍了如何在 Eclipse 中使用 Maven 构建 WAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我现在作为 Maven 项目开始,但由于某种原因它不起作用.这是我的 pom.xml:

I have project that I'm now starting as Maven project, but for some reason it is not working. Here is my 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>

推荐答案

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

Actually, your POM looks a bit weird:

  • 缺少用于 web 应用程序项目的正确打包.
  • maven war 插件配置看起来不对,您只是不需要添加的额外内容.

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

Here is what a minimal pom looks like:

<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>

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

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

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

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-webapp in the wizzard and follow the seps.

或者从命令行:

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

这篇关于如何在 Eclipse 中使用 Maven 构建 WAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆