• Maven使用


    Maven

    1 安装与使用

    修改镜像源,增加 jar 包下载速度,比如使用阿里云的源,settings.xml:

    <mirrors>
      <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>        
      </mirror>
    </mirrors>

    使用 archetype(原型) 插件,生成项目:

    mvn -h
    
    mvn archetype:generate  # interactive
    
    mvn -B archetype:generate
      -DarchetypeGroupId=org.apache.maven.archetypes 
      -DarchetypeArtifactId=maven-archetype-webapp 
      -DgroupId=com.mycompany.app 
      -DartifactId=my-app   # new project one line

    执行命令示例:

    # run task with lifecycle's phase
    mvn install
    mvn clean
    mvn clean install
    mvn clean install -U   # force refresh
    
    # run task with goal directly
    mvn default:install
    mvn archetype:help

    依赖相关命令:

    mvn dependency:help
    
    mvn dependency:list
    mvn dependency:tree
    
    mvn dependency:copy
    mvn dependency:copy-dependencies
    mvn dependency:copy-dependencies -DoutputDirectory=/tmp/xxx -DincludeScope=compile
    
    # 坐标(coordinate): 'groupId:artifactId:version'

     生命周期(Lifecycle)

    包括以下三种

    1. default,默认,用于构建项目:
    2. clean,用于清理
    3. site,用于搭建网站

    default 是默认的生命周期(Lifecycle),它包括很多步骤(Phase),主要的有:

    OrderPhase默认绑定的 Plugin:Goal
    OrderPhase默认绑定的 Plugin:Goal
    1 validate
    2 compile resources:resources, compiler:compile
    3 test resources:testResources, compiler:testCompile, surefire:test
    4 package maven-war-plugin:3.2.0:war
    5 verify
    6 install maven-install-plugin:2.5.2:install
    7 deploy maven-deploy-plugin:2.8.2:deploy

    它的调用方式为:

    mvn install   # 将会依次调用 1-6 的 Phase,即执行每个 Phase 所绑定的 Goal
    mvn compile   # 如果只想编译,并不想测试、打包、安装,那么只需要执行到 compile 即可

    另外,clean 用于清理,包括以下 phase:

    • pre-clean
    • clean
    • post-clean

    site 生命周期的 phase 有:

    • pre-site
    • site
    • post-site
    • site-deploy

    POM 参考

    https://maven.apache.org/pom.html#What_is_the_POM

     POM(Project Object Model) 示例:

    <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>
    
      <!-- The Basics -->
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>my-project</artifactId>
      <version>1.0</version>
      <packaging>jar/war</packaging>
    
      <parent>...</parent>
      <dependencyManagement>...</dependencyManagement>
    
      <!-- Project Settings -->
      <modules>
        <module>xxx</module>
        <module>yyy</module>
      </modules>
    
      <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>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        <xxx.version>1.22222</xxx.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12 | [1.0] | (,1.2] | (,1.0],[1.2,) | LATEST | ${xxx.version}</version>
          <type>jar</type>
          <scope>compile/provided/runtime/test/system</scope>
          <optional>true</optional>
          <exclusions>
            <exclusion>
              <groupId>tgroupid</groupId>
              <artifactId>af</artifactId>
            </exclusion>
          </exclusions>
        </dependency>
      </dependencies>
    
      <!-- Build Settings -->
      <build>...</build>
      <reporting>...</reporting>
    
      <!-- More Project Information -->
      <name>...</name>
      <description>...</description>
      <url>...</url>
      <inceptionYear>...</inceptionYear>
      <licenses>...</licenses>
      <organization>...</organization>
      <developers>...</developers>
      <contributors>...</contributors>
    
      <!-- Environment Settings -->
      <repositories>
        <repository>
          <id>springsource-repo</id>
          <name>SpringSource Repository</name>
          <url>http://repo.springsource.org/release</url>
        </repository>
      </repositories>
      <distributionManagement>
        <repository>
          <id>mycompany-repository</id>
          <name>MyCompany Repository</name>
          <url>scp://repository.mycompany.com/repository/maven2</url>
        </repository>
      </distributionManagement>
      <issueManagement>...</issueManagement>
      <ciManagement>...</ciManagement>
      <mailingLists>...</mailingLists>
      <scm>...</scm>
      <prerequisites>...</prerequisites>
      <pluginRepositories>...</pluginRepositories>
      <profiles>...</profiles>
    </project>

    Build Configuration:

    <build>
      <finalName>xxx</finalName>
    
      <filters>
        <filter>src/main/filters/filter.properties</filter>
      </filters>
    
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>
    
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
      </plugins>
    </build>

     插件配置示例

    插件分为两类:

    • build plugin
    • reporting plugin

    配置示例:

    <plugins>
      <plugin>
        <artifactId>maven-myquery-plugin</artifactId>
        <version>1.0</version>
    
        <configuration>
          <!-- 配置参数 -->
        </configuration>
    
        <dependencies>
          <!-- 配置依赖 -->
        </dependencies>
    
        <executions>
          <!-- 参与 lifecycle -->
          <execution>
            <id>execution1</id>
            <phase>test</phase>
            <goals>
              <goal>query</goal>
            </goals>
            <configuration>
              <url>http://www.foo.com/query</url>
              <timeout>10</timeout>
              <options><option>one</option><option>two</option><option>three</option></options>
            </configuration>
          </execution>
    
          <execution>
            <id>execution2</id>
            <goals><goal>query</goal></goals>
          </execution>
    
        </executions>
      </plugin>
    
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>echodir</id>
            <goals>
              <goal>run</goal>
            </goals>
            <phase>verify</phase>
            <inherited>false</inherited>
            <configuration>
              <tasks>
                <echo>Build Dir: ${project.build.directory}</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

    插件 

    TOMCAT插件:

    <plugin>   
      <groupId>org.apache.tomcat.maven</groupId>   
      <artifactId>tomcat7-maven-plugin</artifactId>   
      <version>2.2</version>   
      <configuration>      
        <hostName>localhost</hostName>    <!-- Default: localhost -->  
        <port>8080</port>                 <!-- Default: 8080 --> 
        <path>/ccc</path>                 <!-- Default: /${project.artifactId}-->  
        <uriEncoding>UTF-8</uriEncoding>  <!-- Default: ISO-8859-1 -->
      </configuration>
    </plugin>

    运行:

    mvn tomcat7:run
    mvn tomcat7:help
    mvn tomcat7:deploy/undeply/redeploy
  • 相关阅读:
    java解析php函数json_encode unicode 编码问题
    python根据域名循环遍历查找绑定IP
    android开发 sqlite设置本地时间
    android判断指定软件是否安装
    android中安装,启动和卸载应用
    .net程序员面试题
    error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
    jQuery EasyUI学习笔记
    [转载]NetBox使用教程!
    Sql一次清空所有数据(基于 Mgo Bcp)
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10075505.html
Copyright © 2020-2023  润新知