maven基础及maven插件

maven基础

优势

  • 相比传统项目,减少了手动导入工序,更加简便
  • 插件丰富
  • 构建简单

安装

  1. maven下载地址
  2. 配置MVN_HOME,如JAVA_HOME类似
  3. 配置完成后,cmd输入mvn-version 检测是否配置成功
  4. 配置setting.xml,路径在maven安装目录conf目录下,打开setting.xml配置一个aliyun的仓库(默认仓库下载速度,以前也可以配置oschina打仓库,现在关闭了)
      <mirrors>
      <!--aliyun仓库-->
    	<mirror>
    	  <id>alimaven</id>
    	  <name>aliyun maven</name>
    	  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    	  <mirrorOf>central</mirrorOf>
    	</mirror>
    	<!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
        </mirror>
    
        <!-- 中央仓库2 -->
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>
        <!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
        <mirror>
          <id>mirrorId</id>
          <mirrorOf>repositoryId</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>http://my.repository.com/repo/path</url>
        </mirror>
         -->
      </mirrors>

maven加载顺序

首先是到用户目录下打.m2目录下setting中去找jar包,再去conf下的setting中找的
maven->/.m2/setting.xml->conf/settiong.xml

pom.xml文件说明

新建maven此处不做说明

  • groupid:公司名(网址)
  • artifactId:模块功能名(common,web,model,dao)
  • version:版本号
  • pachaging:打包方式,默认jar
  • dependencyManagemanet:依赖管理,统一版本号
  • Dependency:依赖
  • Type:默认jar
  • scope:
    • complile:编译
    • test:测试
    • provided:已经提供了,不需要打进包
    • runtime:运行时需要
    • system:本地依赖的jar包
  • exclusions:剔除不需要的jar包,多用于jar包冲突

基本的命令

  • compile:编译(mvn clean:compile)
  • test:运行test
  • package:打包
  • install:install到本地仓库
  • deploy:发布到远程仓库

maven插件

插件网址

常用插件

        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat6-maven-plugin</artifactId>
              <version>2.3-SNAPSHOT</version>
            </plugin>
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.3-SNAPSHOT</version>
            </plugin>
          </plugins>
        </pluginManagement>
  • assembly(打包)
  • zip
  • tar
  • tar.gz (or tgz)
  • tar.bz2 (or tbz2)
  • tar.snappy
  • tar.xz (or txz)
  • jar
  • dir
  • war
  • versions 统一升级版本号
    mvn versions:set -DnewVersion=1.1

自定义插件

1.extends AbstractMojo

    package sample.plugin;
     
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugins.annotations.Mojo;
     
    /**
     * Says "Hi" to the user.
     *
     */
    @Mojo( name = "sayhi")
    public class GreetingMojo extends AbstractMojo
    {
        public void execute() throws MojoExecutionException
        {
            getLog().info( "Hello, world." );
        }
    }

2.pom参数

    <project>
      <modelVersion>4.0.0</modelVersion>
     
      <groupId>sample.plugin</groupId>
      <artifactId>hello-maven-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>maven-plugin</packaging>
     
      <name>Sample Parameter-less Maven Plugin</name>
     
      <dependencies>
        <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-plugin-api</artifactId>
          <version>3.0</version>
        </dependency>
     
        <!-- dependencies to annotations -->
        <dependency>
          <groupId>org.apache.maven.plugin-tools</groupId>
          <artifactId>maven-plugin-annotations</artifactId>
          <version>3.4</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>

3.引用插件

    ...
      <build>
        <plugins>
          <plugin>
            <groupId>sample.plugin</groupId>
            <artifactId>hello-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
          </plugin>
        </plugins>
      </build>
    ...

猜你喜欢

转载自blog.csdn.net/mk33092250/article/details/100109190