Spring mvc maven - 添加库

Spring mvc maven - adding libraries

我正在创建一个 spring mvc 应用程序。我最近遇到了 maven 并计划在我当前的项目中使用它。

我已经下载了 maven 并在我的机器上配置它并使用命令行界面。

我使用以下命令创建了一个项目:

mvn archetype:generate -DgroupId=com.priyank -DartifactId=SpringWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

之后我用依赖项更新了我的 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>com.priyank</groupId>
 <artifactId>SpringWebApp</artifactId>
 <packaging>war</packaging>
 <version>1.0</version>
 <name>SpringWebApp Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<!-- Spring dependencies -->

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-instrument</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-instrument-tomcat</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jms</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-messaging</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

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

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc-portlet</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-websocket</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

<!-- hibernate dependencies -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.3.8.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.8.Final</version>
</dependency>
 </dependencies>
 <build>
   <finalName>SpringWebApp</finalName>
 </build>
</project>

从我的命令行更新 pom.xml 后,我执行了:

mvn install

它下载了我本地存储库中的所有依赖项。

之后,我使用命令将其转换为我的 eclipse 项目:

mvn eclipse:eclipse -Dwtpversion=2.0

现在在我的工作区中导入此项目 SpringWebApp 后,我可以看到以如下方式包含的库:

通常当我在没有 Maven 的情况下创建 Spring mvc 时,我将我的库放在 WEB-INF\lib 文件夹下。

我在这里很困惑。好像我做错了什么。将此库包含在项目中的正确方法是什么?

P.S.: 这添加了 jre 1.4 作为我的运行时,但我已将其更改为 1.7

我更喜欢使用包含在 eclipse 中的 m2e 插件来制作 .classpath / .project 和 .settings 的 eclipse 配置然后 eclipse:eclipse 目标。

当您使用 m2e 插件类路径库配置 eclipse 项目时,如下所示:

并且所有依赖项都由 eclipse 自动部署到 WEB-INF/lib 中,并通过 maven-war-plugin 部署到 war 中。

强制使用特定版本的 java 你需要在 maven-compiler-plugin 的配置中指定版本,如下所示:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
</plugin>