Java 新 Maven / Spring MVC 项目的最小 pom.xml 文件

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

Minimum pom.xml file for new Maven / Spring MVC project

javaeclipsespringmavenpom.xml

提问by Myna

I am completely new to Maven and Spring MVC. What I am looking to do is to set up a new Spring MVC project, using Maven (hopefully this sentence makes sense), and run my web app on Tomcat using Eclipse.

我对 Maven 和 Spring MVC 完全陌生。我想要做的是建立一个新的 Spring MVC 项目,使用 Maven(希望这句话有意义),并使用 Eclipse 在 Tomcat 上运行我的 Web 应用程序。

I am following the tutorial at this link and I have a problem with the pom.xmlfile:

我正在关注此链接上的教程,但pom.xml文件有问题:

The tutorial asks to create the folder structure, and to create a pom.xml file. For starters they ask to put this line in pom.xml:

本教程要求创建文件夹结构,并创建一个 pom.xml 文件。对于初学者,他们要求将此行放在 pom.xml 中:

xml 4.0.0org.springsource.greenbeans.mavenexample11.0-SNAPSHOTOur Simple Project

But when I do that, my Eclipse throws an error:

但是当我这样做时,我的 Eclipse 会抛出一个错误:

[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project  (D:\projectName\pom.xml) has 1 error
[ERROR]     Non-parseable POM D:\projectName\pom.xml: only whitespace content allowed before start tag and not ` (position: START_DOCUMENT seen `... @1:1)  @ line 1, column 1 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException

Any idea on what the right minimum pom.xml could be? I tried many things but it didn't work for me...

知道什么是正确的最小 pom.xml 吗?我尝试了很多东西,但它对我不起作用......

回答by Amit Sharma

You need not create folder structure yourself. Maven archtype 'maven-archetype-webapp' does it for you. Here is a good tutorial to guide you through for basic setting up a MVC project in eclipse (and the right approach):

您无需自己创建文件夹结构。Maven archtype 'maven-archetype-webapp' 为你做这件事。这是一个很好的教程,可指导您在 Eclipse 中基本设置 MVC 项目(以及正确的方法):

  • Create Spring MVC dynamic web project with Maven and make it support Eclipse IDE

    <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>com.beingjavaguys.sample</groupId>
    <artifactId>SampleSpringMaven</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SampleSpringMaven Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <jdk.version>1.6</jdk.version>
    </properties>
    
    <dependencies>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>SampleSpringMaven</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>my-tomcat</server>
                    <path>/SampleSpringMaven</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

  • 使用Maven创建Spring MVC动态Web项目并使其支持Eclipse IDE

    <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>com.beingjavaguys.sample</groupId>
    <artifactId>SampleSpringMaven</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SampleSpringMaven Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <jdk.version>1.6</jdk.version>
    </properties>
    
    <dependencies>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>SampleSpringMaven</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>my-tomcat</server>
                    <path>/SampleSpringMaven</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

回答by Helder Pereira

The minimal pom.xmlfile for Spring MVC with Tomcat could be:

pom.xml带有 Tomcat 的 Spring MVC的最小文件可能是:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.maven</groupId>
    <artifactId>minimal-spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And then:

进而:

$ mvn -Dmaven.tomcat.port=8888 tomcat7:run