美文网首页
maven使用小记

maven使用小记

作者: 九号自行车司机 | 来源:发表于2019-04-24 17:40 被阅读0次

    下载安装

    下载

    打开官方下载链接https://maven.apache.org/download.cgi


    笔者用的是windows系统,所以点击红框选中的zip压缩包,下载即可。

    安装

    1. 确定JAVA_HOME已经添加到了系统环境变量。
    2. 解压文件


    3. bin目录添加到系统的PATH环境变量。
    4. 输入mvn -v,得到下图的输出,证明maven安装成功。

    Hello World

    1. 选择一个目录,执行下面的命令
    mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
    

    可以看到生成了一个目录my-app,而且目录里的文件如图所示

    1. 打开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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>my-app</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    

    官网对这些配置进行了简单的解释:

    • project This is the top-level element in all Maven pom.xml files.
    • modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
    • groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plugins.
    • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
    • packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
    • version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
    • name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
    • url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
    • description This element provides a basic description of your project. This is often used in Maven's generated documentation.

    想要更深入了解POM,可以参考Introduction to the POM

    1. 常用指令

    编译

    mvn compile
    

    打包

    mvn package
    

    安装到本地仓库(${user.home}/.m2/repository

    mvn install
    

    clean

    mvn clean
    

    配置

    jdk版本

    maven默认使用的编译版本是1.5,打开conf/settings.xml文件并编辑它

    <profiles>
      <profile>
        <id>development</id>
        <activation>
          <jdk>1.8</jdk>
          <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
          <maven.compiler.source>1.8</maven.compiler.source>
          <maven.compiler.target>1.8</maven.compiler.target>
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
      </profile>
    </profiles>
    

    添加如上配置,编译版本变为1.8

    使用阿里云作镜像

    编辑conf/settings.xml文件

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

    知识点

    排除依赖

    使用<exclusions>

    <project>
      ...
      <dependencies>
        <dependency>
          <groupId>sample.ProjectA</groupId>
          <artifactId>Project-A</artifactId>
          <version>1.0</version>
          <scope>compile</scope>
          <exclusions>
            <exclusion>  <!-- declare the exclusion here -->
              <groupId>sample.ProjectB</groupId>
              <artifactId>Project-B</artifactId>
            </exclusion>
          </exclusions> 
        </dependency>
      </dependencies>
    </project>
    

    打包源码

    使用插件maven-source-plugin

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
              <execution>
                <id>attach-source</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    使用profiles

    <project>
      ...
      <profiles>
        <profile>
          <id>env-dev</id>
          <properties>
            <env>dev</env>
          </properties>
          <activation>
            <activeByDefault>true</activeByDefault>
          </activation>
        </profile>
    
        <profile>
          <id>env-test</id>
          <properties>
            <env>test</env>
          </properties>
        </profile>
     
        <profile>
          <id>env-prod</id>
          <properties>
            <env>prod</env>
          </properties>
        </profile>
      </profiles>
      ..
    </project>
    

    执行mvn install -P env-prodmvn -Denv=prod install,id为env-prod的profile被激活。
    结合对resources的配置,可以在激活不同的profile时,让项目使用不同的配置文件:

    <project>
     ...
     <build>
       ...
       <resources>
         <resource>
           <directory>src/main/resources/${env}</directory>
         </resource>
       </resources>
       ...
     </build>
     ...
    </project>
    

    设置文件编码

    <project ...>
     ...
     <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       ...
     </properties>
     ..
    </project>
    

    跳过单元测试

    命令行执行

    mvn install -DskipTests
    

    mvn install -Dmaven.test.skip=true
    

    后者不止会跳过单元测试,还不会对测试代码进行编译。
    也可以选择直接把属性写在配置文件:

    <project>
      [...]
      <properties>
        <skipTests>true</skipTests>
      </properties>
      [...]
    </project>
    

    <project>
      [...]
      <properties>
        <maven.test.skip>true</maven.test.skip>
      </properties>
      [...]
    </project>
    

    问题

    • maven install报错Cannot run program "gpg.exe": CreateProcess error
      mvn install后加上参数-Dgpg.skip,例如:
    mvn install -Dgpg.skip
    

    • maven install报错MavenReportException: Error while generating Javadoc
      mvn install后加上参数-Dmaven.javadoc.skip=true,例如:
    mvn install -Dmaven.javadoc.skip=true
    

    参考资料

    相关文章

      网友评论

          本文标题:maven使用小记

          本文链接:https://www.haomeiwen.com/subject/erbggqtx.html