码迷,mamicode.com
首页 > 其他好文 > 详细

maven——项目构建和依赖管理工具

时间:2019-02-27 11:43:17      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:location   home   私有   4.0   项目文件   3.0.0   dcl   rem   unzip   

apache maven是一个用于项目构建和依赖管理的工具。

添加archetype

https://repo1.maven.org/maven2/archetype-catalog.xml

更改本地仓库存储位置:修改配置文件${user.home}/.m2/settings.xml或 $MAVEN_HOME/conf/setting.xml中的内容

<localRepository>...

mvn archetype:generate会非常慢,搜索了本地和远程太多archetype,而加上-DarchetypeCatalog=local将只搜索${user.home}/.m2/archetype-catalog.xml中定义的本地archetype,会大大提高速度。

mvn archetype:crawl会根据远程数据生成本地archetype目录$maven_localRepository/archetype-catalog.xml。

download jar, sources, javadoc

 mvn dependency:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]
 eg.    mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST
        mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:sources

download sources/javadoc

 mvn dependency:sources -DincludeGroupIds=grouId1,groupId2... -Dclassifier=sources
 mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:sources
 
download sources for each of dependencies in pom.xml
 mvn dependency:sources     
 mvn dependency:resolve -Dclassifier=sources
download javadoc
 mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:javadoc
 # download javadoc for each of dependencies in pom.xml
 mvn dependency:sources -Dclassifier=javadoc
 mvn dependency:resolve -Dclassifier=javadoc

copy dependecies

mvn dependency:copy -Dartifact=g:a:v -o -DrepoUrl=file://path/to/your/repo
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=groupId:artifactId:version[:packaging][:classifier] -DoutputDirectory=[target] 

输出依赖jar包文件路径

mvn dependency:build-classpath [-Dmdep.outputFile=/path/file](输出到文件)

mvn基本的几个jar包相关命令:
mvn clean 清除target/编译文件夹
mvn compile 编译源码
mvn package 编译并打包为jar文件
mvn install 将jar文件安装到本地repo
mvn deploy 根据pom.xml中的信息,将jar包部署到远程repo

跳过测试
mvn -DskipTests

指定pom文件
mvn -f some-pom.xml

插件描述文档

mvn help:describe -Ddetail -Dplugin=<plugin-name> 或 -Dcmd=<plugin>:<phase>
# mvn help:describe -Dplugin=exec
# mvn help:describe -Dcmd=install:install-file
# mvn help:describe -Dplugin=g:a:v

关闭mvn的日志输出
mvn -q

生成源码jar包
mvn source:jar

maven 3.x super pom位置
$M2_HOME/lib/maven-model-builder-3.x.jar:/org/apache/maven/model/pom-4.0.0.xml

unresolved

list addable dependencies in local repository
list addable dependencies all available repositories
list achetypes

(不可用)get file path of artifact
mvn dependency:location -Dartifact=g???v

生成javadoc.jar接口文档包

mvn javadoc:jar
需要添加plugin: org.apache.maven.plugins:maven-javadoc-plugin

生成sources.jar源码包

mvn source:jar
需要添加plugin: org.apache.maven.plugins:maven-source-plugin

mvn命令发布到nexus私服
mvn deploy
如果需要同时发布sources.jar, javadoc.jar,需在生成这些包后执行命令。
执行命令需要pom中有<distributionManagement/repository|snapshotRepository>的配置,同时maven公有settings.xml中下配置nexus私服登录的用户名密码。

<!-- pom.xml -->
<distributionManagement>
        <repository>
            <id>my-mvn</id> <!-- 必须与settings.xml中定义的id一致 -->
            <!--为hosted服务类型的地址,否则400错误,另外,部署一个已存在的jar包也会400 -->
            <url>http://exmaples.com/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>my-mvn</id>
            <url>http://exmaples.com/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
</distributionManagement>

<!-- maven settings.xml -->
<servers>
  <server>
      <id>my-mvn</id>
      <username>admin</username>
      <password>secret-pwd</password>
  </server>
</servers>

<!-- 发布源码包/文档包时依赖的plugin -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

删除损坏jar包

remove damaged jars.
网络条件恶劣会导致下载的jar是不完整的或者损坏的(corrupted),而添加这种jar包的依赖会导致读取到不正确的class(加了jar包却找不到对应class),此时我们需要找出本地maven库中损坏的jar,然后删除它们,重新下载,命令行脚本:

cd $M2_REPO;
for f in `find -type f -iname '*.jar' -printf '%P\n'`; do unzip -tq "$f" &>/dev/null; [[ $? -ne 0 ]] && echo "$f" | tee -a ~/corruptedmvnjar ; done;
cd -

打包mave项目及其依赖

一般建议用功能更丰富的shade插件,而非assembly。

编译出错

仔细看输出ERROR日志,如提示某个类miss,除了看是否添加了该类的依赖,scop是否对应以外,还需看这个类所在依赖是否使用了其他依赖,而其他依赖却没有添加,或者版本不兼容。

打包到nexus:

pom.xml

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <name>Nexus Release Repository</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

增量复制maven库

cd $M2_REPO
zip -r -u ~/Downloads/mvnrepo.zip *  # 可加选项排除部分大ja包

maven仓库镜像

添加仓库镜像,解决下载慢的问题

<!--阿里云-->
<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
</mirror>
<!--华为云-->
    <mirror>
      <id>huaweicloud</id>
      <!--官方将这里的mirrorOf设为*,会导致私库不可用-->
      <mirrorOf>central</mirrorOf>
      <url>https://mirrors.huaweicloud.com/repository/maven/</url>
    </mirror> 
<!--华为云镜像登录信息-->
    <server>
      <id>huaweicloud</id>
      <username>anonymous</username>
      <password>devcloud</password>
    </server> 

镜像会拦截所有通往被镜像的请求。因此若镜像更新不及时,导致镜像中没有本已存在于被镜像repo(如maven central)的artifact时,mvn下载会失败。

maven私有库
--
添加私有库 settings.xml

<profile>
 <id>...</id>
 <repositories>...
 <pluginRe...>
 
</profile>

maven项目添加自己提供的jar:

修改plugin默认版本

修改文件$M2_HOME/lib/maven-core-xxx.jar!:/META-INF/plexus/components.xml及default-bindings.xml中对应plugin的版本

maven java

pom.xml中最常用的设置jdk、项目文件编码的代码如下:

<properties>
    <!--设置java源码版本-->
    <maven.compiler.source>1.8<maven.compiler.source>
    <!--设置字节码版本-->
    <maven.compiler.target>1.8<maven.compiler.target>
    <!--在maven-compiler-plugin:3.8以后支持jdk9+,设置maven.compiler.release,同时显式设置maven-compiler-plugin的版本为3.8或更高-->
    <maven.compiler.release>11</maven.compiler.release>
    <!--如果在<build/plugins>显式配置了<artifactId>maven-compiler-plugin</artifactId>中的<execution>,则上述通过propert指定java版本的方式不再起作用,需在<execution/configuration>中配置<source>,<target>-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

maven scala

<project>
<!--repo&pluginRepo不是非得添加-->
  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>
  
  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>
  </dependencies>
<build>
    <plugins>
    <!-- maven-scala-plugin非官方出品,已迁移到以下gav -->
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>scala-compile-first</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>add-source</goal>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>scala-test-compile</id>
                <phase>process-test-resources</phase>
                <goals>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
      </plugin>
      <!--加java编译器-->
      <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
      </plugin>
      <!--<plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
          <mainSourceDir>src/main/scala</mainSourceDir>
          <testSourceDir>src/test/scala</testSourceDir>
        </configuration>
      </plugin>-->
    </plugins>
  </build>
</project>

kotlin maven

<properties>
    <!--增量编译-->
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
</properties>

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <!--使用jdk特定版本扩展-->
    <!--<artifactId>kotlin-stdlib-jre8</artifactId>-->
    <!--<artifactId>kotlin-stdlib-jre7</artifactId>-->
    <version>${kotlin.version}</version>
</dependency>
<!--kotlin reflect反射相关-->
<dependency>
    <artifactId>kotlin-reflect</artifactId>
</dependency>

shade-plugin:

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <!--别生成dependency-reduced-pom.xml文件-->
        <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <!-- 加入package和goal:shade,否则mvn shade:shade不允许直接从命令行执行,加此可直接mvn package -->
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <!--将同名资源文件整合成一个-->
                    <transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
          <resource>application.properties</resource>
                    </transformer>
                     <!-- 定义manifest中main类 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                     <mainClass>com.examples.AppMain</mainClass>
                     <manifestEntries><!--manifest文件中的款项配置-->
                        <Class-Path>xxx</Class-Path>
                    </manifestEntries>
                    </transformer>
                </transformers>
                <!-- 以下排除依赖包中的*.SF *.DSA *.RSA文件,避免运行jar包时报告关于signature不匹配的安全问题 -->
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                           <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

maven——项目构建和依赖管理工具

标签:location   home   私有   4.0   项目文件   3.0.0   dcl   rem   unzip   

原文地址:https://www.cnblogs.com/xmaples/p/10442565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!