【问题标题】:How can I build WAR with Maven in Eclipse?如何在 Eclipse 中使用 Maven 构建 WAR?
【发布时间】:2011-03-08 04:38:13
【问题描述】:

我有一个项目,我现在开始作为 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>

【问题讨论】:

  • 究竟是什么不工作,你有错误信息还是什么?
  • 我怎样才能建立战争?我的意思是我需要战争文件

标签: java maven-2 web-applications


【解决方案1】:

其实你的 POM 看起来有点怪:

  • webapp 项目缺少正确的packaging
  • maven war 插件配置看起来不正确,您只是不需要添加的额外内容。

这是一个最小的 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>

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

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

或者从命令行:

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

【讨论】:

  • 元素 finalName 不是必需的,它是 -.
  • 谢谢,我从构建中删除了所有内容,现在它似乎可以工作了。
  • @newbie:不客气。请注意,您需要 Java 6 的 maven 编译器设置。
【解决方案2】:

例如,我看到了

<packaging>war</packaging>

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • m2eclipse 没有将 maven 与 WTP 集成。对于集成,您需要使用 eclipse.org/m2e-wtp
    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多