Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build WAR with Maven in Eclipse?

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>
like image 707
newbie Avatar asked Jun 25 '10 08:06

newbie


People also ask

How do you create a war in Maven?

Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory. This is a normal directory, and all the files inside the WAR file are contained inside the exploded WAR directory.

How do I run a Maven build in Eclipse?

Building and Running the Maven Project in Eclipse To run the maven project, select it and go to “Run As > Java Application”. In the next window, select the main class to execute. In this case, select the App class and click on the Ok button. You will see the “Hello World” output in the Console window.

How do I create a WAR file?

To create war file, you need to use jar tool of JDK. You need to use -c switch of jar, to create the war file. Go inside the project directory of your project (outside the WEB-INF), then write the following command: jar -cvf projectname.


2 Answers

Actually, your POM looks a bit weird:

  • it is missing the right packaging for a webapp project.
  • the maven war plugin configuration doesn't look right, you just don't need the extra stuff you added.

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>

So either modify it and update the project configuration (right-click on your project then 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-webapp in the wizzard and follow the seps.

Or from the command line:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
like image 103
Pascal Thivent Avatar answered Sep 16 '22 16:09

Pascal Thivent


for instance, I see

<packaging>war</packaging>

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

like image 34
xor_eq Avatar answered Sep 19 '22 16:09

xor_eq