首页投稿(暂停使用,暂停投稿)技术干货

Maven学习

2016-09-09  本文已影响670人  MPPC

在项目中Maven用的一直比较多,以前只知道简单的配置一些依赖,所以找了时间孔浩老师Maven的学习视频学习了一下

Maven初步-手动建立 Maven 项目

1、示例
    maven-ch01/pom.xml
    maven-ch01/src/main/java/......
    maven-ch01/src/main/resources/...
    maven-ch01/src/test/java/......
    maven-ch01/src/test/resources/...
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/xsd/maven-4.0.0.xsd">
    
    <!--modelVersion 这是固定值不用修改 -->
    <modelVersion>4.0.0</modelVersion>
    
    <!-- GAV 模块唯一的坐标,groupId 一般表示项目 -->
    <groupId>org.panpan.hellomaven</groupId>
    <!--模块名称 -->
    <artifactId>maven-ch01</artifactId>
    <!-- 版本号 -->
    <version>0.0.1-SNAPSHOT</version>
    
    <dependencies>
        <!-- 这里放的是依赖的模块 -->
    </dependencies>
</project>
mvn compile   //编译
mvn test      //测试
mvn package   //打包
mvn install   //安装到本地仓库
mvn deploy    //发布到仓库中

Eclipse 建立 Maven 项目

Maven 中的依赖特性

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>user-log</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 排重 user-log 模块中的 commoms-logging 依赖 -->
    <exclusions>
        <exclusion>
            <groupId>commoms-logging</groupId>
            <artifactId>commoms-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Maven 依赖范围

<!-- 测试范围的依赖 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<!-- 运行的时候依赖  -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
    <scope>runtime</scope>
</dependency>

Maven的聚合和继承

示例: 聚合和继承的路径

user-maven/pom.xml
user-maven/user-core
user-maven/user-log
user-maven/user-service
<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.panpan.hellomaven</groupId>
    <artifactId>user-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 类型为pom 管理 -->
    <packaging>pom</packaging>
    
    <!-- module 聚合里面存放是项目模块的绝对路径  -->
    <modules>
        <module>user-dao</module>
        <module>user-log</module>
        <module>user-service</module>
    </modules>
</project>
user-maven 中的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.panpan.hellomaven</groupId>
    <artifactId>user-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 类型为pom 管理 -->
    <packaging>pom</packaging>

    <url>http://maven.apache.org</url>

    <!-- module 聚合里面存放是项目模块的绝对路径 -->
    <modules>
        <module>user-dao</module>
        <module>user-log</module>
        <module>user-service</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
        <lombok.version>1.16.10</lombok.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.5.2</slf4j.version>
        <dbunit.version>2.4.5</dbunit.version>
        <mysql.version>5.1.38</mysql.version>
        <hibernate.version>4.3.11.Final</hibernate.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.dbunit</groupId>
                <artifactId>dbunit</artifactId>
                <version>${dbunit.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
在user-dao 中继承 user-maven 的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--  继承user-maven 的pomx.ml -->
    <parent>
        <groupId>org.panpan.hellomaven</groupId>
        <artifactId>user-maven</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!-- 继承的绝对路径是pom的文件, 如果是在外层可以不添加这个配置 -->
        <!-- 或默认继承在该模块的最外层根目录下 -->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>org.panpan.hellomaven</groupId>
    <artifactId>user-dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>user-dao</name>
    
    <dependencies>
        <!-- 对于继承过来的,可以不用写上版本号继承 user-maven -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
    </dependencies>
</project>

Nexus repository

nexus 介绍和安装
nexus的配置
<profiles>
  <profile>
    <id>nexusProfile</id>
    
    <repositories>
      <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8080/nexus/content/groups/public/</url>
        <!-- 可以下载releases Jar 包 默认是true -->
        <releases>
          <enabled>true</enabled>
        </releases>
        <!-- 可以下载snapshots Jar包默认是false -->
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
      </repository>
    </repositories>
  
  </profile>
</profiles>


<!-- 在这激活哪个profile id,哪个就生效 -->
<activeProfiles>
  <activeProfile>nexusProfile</activeProfile>
</activeProfiles>
<mirrors>
    <!-- 工厂的镜像,只要mirrorOf 中的工厂要访问,都会自动来找镜像,就不会再去重要工厂 -->
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <!-- 也可以配置为 * 来代替-->
      <mirrorOf>nexus,central</mirrorOf>
      <url>http://192.168.0.221:8081/nexus/content/groups/public</url>
    </mirror>   
  </mirrors>

<profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
      <!-- 表示配置中央工厂中的snapshots为true,查找地址为上面配置mirror 镜像 -->
          <repository>
            <id>central</id>
            <url>http://central</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </repository>
        </repositories>
    </profile>
</profiles>

<activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
</activeProfiles>  
发布项目到nexus
<!-- pom.xml -->
<!-- 只要在parent 模块中配置,后面模块都继承就行 -->
<distributionManagement>
  <repository>
    <id>nexus-releases</id>
      <name>Nexus Release Repository</name>
      <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus-snapshots</id>
    <name>Nexus Snapshot Repository</name>
    <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
  </snapshotRepository>
</distributionManagement>
<!-- setting.xml -->
<servers>
  <server>
    <id>nexus-releases</id>
    <username>admin</username>
    <password>admin123</password>
  </server>
  <server>
    <id>nexus-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
  </server>  
</servers>

生命周期和插件

maven 的三套生命周期
      validate
      generate-sources
      process-sources
      generate-resources
      process-resources     复制并处理资源文件,至目标目录,准备打包。
  compile     编译项目的源代码。 //到这一步完成 compile
      process-classes
      generate-test-sources 
      process-test-sources 
      generate-test-resources
      process-test-resources     复制并处理资源文件,至目标测试目录。
      test-compile     编译测试源代码。
      process-test-classes
  test     使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
      prepare-package
  package     接受编译好的代码,打包成可发布的格式,如 JAR 。
      pre-integration-test
      integration-test
      post-integration-test
      verify
  install     将包安装至本地仓库,以让其它项目依赖。
  deploy     将最终的包复制到远程的仓库,以让其它开发人员与项目共享。
Maven 插件
Maven 插件介绍
Maven 插件基础
Maven Plugin | center
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java_source_version>1.8</java_source_version>
    <java_target_version>1.8</java_target_version>
    <maven_compiler_plugin_version>3.3</maven_compiler_plugin_version>
    <maven_source_plugin_version>2.4</maven_source_plugin_version>
    <maven_jar_plugin_version>2.5</maven_jar_plugin_version>
    <maven_war_plugin_version>2.5</maven_war_plugin_version>
    <maven_deploy_plugin_version>2.8.2</maven_deploy_plugin_version>
    <spring.version>4.2.1.RELEASE</spring.version>
</properties>
<build>
<plugins>
......

<!--示例—1: 指定编译的JDK 版本号 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven_compiler_plugin_version}</version>
    <configuration>
        <source>${java_source_version}</source>
        <target>${java_target_version}</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

<!--示例-2:生成的Java源文件的Jar包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>${maven_source_plugin_version}</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <!-- 指定插件的目标  -->
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <attach>true</attach>
    </configuration>
</plugin>

<!-- 示例-3: 生成Maven War包-->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>${maven_war_plugin_version}</version>
   <configuration>
       <archive>
           <addMavenDescriptor>true</addMavenDescriptor>
       </archive>
   </configuration>
</plugin>

<!-- 示例-4: Build a JAR from the current project. -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven_jar_plugin_version}</version>
    <configuration>
        <archive>
            <addMavenDescriptor>true</addMavenDescriptor>
        </archive>
    </configuration>
</plugin>
......
</plugins>
</build>
上一篇下一篇

猜你喜欢

热点阅读