【问题标题】:Minimum pom.xml file for new Maven / Spring MVC project新 Maven / Spring MVC 项目的最小 pom.xml 文件
【发布时间】:2014-01-09 18:06:33
【问题描述】:

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

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

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

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

但是当我这样做时,我的 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

你知道 pom.xml 的最小值是多少吗?我尝试了很多东西,但它对我不起作用......

【问题讨论】:

    标签: java eclipse spring maven pom.xml


    【解决方案1】:

    这是用于非 Spring Boot 应用程序的最小 pom.xml,它将生成一个 Spring 5、Java 11 的 war 文件,您可以将其部署在 tomcat 上。我在依赖管理中使用了spring bom,它锁定了spring框架jar的版本

    <?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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.yourco</groupId>
        <artifactId>spring-mvc-demo</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-security Maven Webapp</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>11</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <!-- https://mvnrepository.com/artifact/org.springframework/spring-framework-bom -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-framework-bom</artifactId>
                    <version>5.3.9</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>spring-mvc-demo</finalName>
            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java-version}</source>
                        <target>${java-version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.2</version>
                </plugin>
        </build>
    </project>
    

    【讨论】:

      【解决方案2】:

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

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

      然后:

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

      【讨论】:

        【解决方案3】:

        您不需要自己创建文件夹结构。 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>
          

        【讨论】:

        • @Myna 乐于助人:)
        • 嘿,我对“在 Eclipse 中运行 Spring Maven 项目”部分有疑问:我只看到 junit。该命令的跟踪如下: [...] 使用 Eclipse 工作区:null 添加默认类路径容器:org.eclipse.jdt.launching.JRE_CONTAINER 不写设置 - 默认足够 文件 D:\Users\FirstMavenProject\.project 已经存在。将保留其他设置,如果要删除旧设置,请运行 mvn eclipse:clean。将“FirstMavenProject”的 Eclipse 项目写入 D:\Users\FirstMavenProject。知道为什么我没有得到所有的 .jars 吗?
        • 如果你在使用 maven,我强烈推荐使用 Intellij Idea 而不是 Eclipse。在 Idea 中,您所要做的就是打开 pom.xml,并在询问时选择打开相关项目。让您的生活更简单。
        猜你喜欢
        • 2019-02-15
        • 1970-01-01
        • 2017-04-08
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多