使用maven构建多模块项目(二)

来源:互联网 发布:19s管理淘宝 编辑:程序博客网 时间:2024/06/04 19:36
  1. 使用dependencyManagement管理依赖
  2. 使用pluginManagement管理插件
  3. 定义项目属性及配置信息

1. 使用dependencyManagement管理依赖
dependencyManagement的作用:Maven使用dependencyManagement元素来提供一种管理依赖版本号的方法。

(1)helloweb-parent——>pom.xml,添加以下内容

<dependencyManagement>    <dependencies>        <dependency>           <groupId>javax.servlet</groupId>           <artifactId>javax.servlet-api</artifactId>           <version>3.0.1</version>           <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.1</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.21</version>            <scope>compile</scope>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-api</artifactId>            <version>2.2</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-core</artifactId>            <version>2.2</version>        </dependency>    </dependencies>  </dependencyManagement>

(2)执行maven install

这样之后在子项目中添加相应的依赖包就不需要再指定版本了

(3)helloweb-entity——>pom.xml
添加以下内容

<dependencies>    <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <scope>test</scope>        </dependency>  </dependencies>

(4)maven install

2. 使用pluginManagement管理插件
pluginManagement的作用:Maven使用pluginManagement元素来提供一种管理插件的方式
(1)helloweb-parent——>pom.xml,添加以下内容

 <build>    <pluginManagement>        <plugins>            <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-compiler-plugin</artifactId>                    <version>2.3.2</version>                    <configuration>                        <source>1.7</source>                        <target>1.7</target>                        <encoding>UTF-8</encoding>                    </configuration>                </plugin>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-resources-plugin</artifactId>                    <version>2.6</version>                </plugin>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-war-plugin</artifactId>                    <version>2.4</version>                    <configuration>                        <!-- 设定包文件的名称,不带具体的版本号 -->                        <warName>${project.artifactId}</warName>                    </configuration>                </plugin>        </plugins>    </pluginManagement>  </build> 

这段的配置会被应用到所有子模块的编译插件中。

(2)发现多个模块中有错误,全部选中,update project

3. 定义项目属性及配置信息
(1)helloweb-parent——>pom.xml添加以下内容

<properties>    <jdk.version>1.7</jdk.version>    <servlet.api.version>3.0.1</servlet.api.version>    <jsp.api.version>2.1</jsp.api.version>    <junit.version>4.11</junit.version>    <mysql.version>5.1.21</mysql.version>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>

这样就可以使用比如${servlet.api.version}的格式去替代之前的version硬编码

(2)maven install

原创粉丝点击