常用Maven收集以及Maven技巧

来源:互联网 发布:windows好用的v翻墙 编辑:程序博客网 时间:2024/05/16 17:16

1.完整的Maven的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>org.me</groupId>    <artifactId>main</artifactId>    <version>2.0.0</version>    <packaging>jar</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <maven.compiler.source>1.8</maven.compiler.source>        <maven.compiler.target>1.8</maven.compiler.target>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>        </dependency>        <!-- Apache Commons Lang -->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.3.2</version>        </dependency>        <!-- Apache Commons Collections -->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-collections4</artifactId>            <version>4.0</version>        </dependency>               <dependency>            <groupId>commons-collections</groupId>            <artifactId>commons-collections</artifactId>            <version>3.2.2</version>        </dependency>        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.2</version>        </dependency>        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpclient</artifactId>            <version>4.5.2</version>        </dependency>        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpcore</artifactId>            <version>4.4.4</version>        </dependency>        <dependency>            <groupId>commons-io</groupId>            <artifactId>commons-io</artifactId>            <version>2.4</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.7.18</version>        </dependency>        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>cglib</groupId>            <artifactId>cglib-nodep</artifactId>            <version>3.2.0</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.8.8</version>        </dependency>        <dependency>            <groupId>aopalliance</groupId>            <artifactId>aopalliance</artifactId>            <version>1.0</version>        </dependency>           </dependencies>    <build>        <plugins>            <plugin>                  <artifactId>maven-assembly-plugin</artifactId>                  <configuration>                      <appendAssemblyId>false</appendAssemblyId>                      <descriptorRefs>                          <descriptorRef>jar-with-dependencies</descriptorRef>                      </descriptorRefs>                      <archive>                          <manifest>                              <mainClass>com.me.main.MainJFrame</mainClass>                          </manifest>                      </archive>                  </configuration>                  <executions>                      <execution>                          <id>make-assembly</id>                          <phase>package</phase>                          <goals>                              <goal>assembly</goal>                          </goals>                      </execution>                  </executions>              </plugin>        </plugins>    </build></project>



注意:

使用maven默认的package命令构建的jar包中只包括了工程自身的class文件,并没有包括依赖的jar包。我们可以通过配置插件来对工程进行打包,pom具体配置如下:

            <plugin>                  <artifactId>maven-assembly-plugin</artifactId>                  <configuration>                      <appendAssemblyId>false</appendAssemblyId>                      <descriptorRefs>                          <descriptorRef>jar-with-dependencies</descriptorRef>                      </descriptorRefs>                      <archive>                          <manifest>                              <mainClass>com.me.main.MainJFrame</mainClass>                          </manifest>                      </archive>                  </configuration>                  <executions>                      <execution>                          <id>make-assembly</id>                          <phase>package</phase>                          <goals>                              <goal>assembly</goal>                          </goals>                      </execution>                  </executions>              </plugin>


参考:使用maven插件对java工程进行打包


2.Maven模块划分



建立一个父模块mavenprojects

<?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.hm</groupId>    <artifactId>mavenprojects</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>pom</packaging>    <modules>        <module>projectclient</module>        <module>projectserver</module>    </modules>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties></project>


建立2个子项目projectclient和projectserver


projectserver有一个类

public class NewT {    public int add(int a, int b) {        return a + b;    }}

projectclient需要依赖此项目

<?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>    <parent>        <groupId>com.hm</groupId>        <artifactId>mavenprojects</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <artifactId>projectclient</artifactId>    <packaging>jar</packaging>    <properties>        <maven.compiler.source>1.8</maven.compiler.source>        <maven.compiler.target>1.8</maven.compiler.target>    </properties>    <dependencies>          <dependency>              <groupId>com.hm</groupId>              <artifactId>projectserver</artifactId>              <version>${project.version}</version>          </dependency>      </dependencies></project>


projectclient调用
package com.hm.projectclient;import com.hm.projectserver.NewT;public class NewApp {    public static void main(String[] args) {        NewT t = new NewT();        System.out.println(t.add(1, 2));    }}

参考:Maven最佳实践:划分模块
0 0
原创粉丝点击