不安分的黑娃
踏踏实实,坚持学习,慢慢就懂了~

Maven 使用

1、如何正确使用多个仓库的镜像?

有个需求:v5产品依赖包使用 v5仓库,百度OCR SDK 包使用 aliyun 仓库。

解决方案:使用 aliyun 仓库镜像覆盖工程 pom 里自定义仓库,这样就能满足需求。

参考博客: https://blog.csdn.net/q42368773/article/details/107216016

1.1 工程pom文件增加自定义仓库 myrepo

<repositories>
    <repository>
        <id>myrepo</id>
        <name>my maven repository</name>
        <url>https://myrepo.com/</url>
    </repository>
</repositories>

1.2 settings.xml 添加 aliyun 镜像覆盖 自定义仓库 myrepo

<mirrors>
    <!-- v5 仓库镜像 -->
	 <mirror>
    <id>distributedComprehensiveCredit-v5</id>
      <mirrorOf>*</mirrorOf>
      <url>http://192.168.50.11:8081/repository/distributedComprehensiveCredit-v5/repositoryv5/</url>
    </mirror>
    
    <!-- aliyun 仓库镜像替代 myrepo -->
   <mirror>
      <id>alimaven</id>
      <mirrorOf>myrepo</mirrorOf>
      <name>alibaba maven repository</name>
      <url>https://maven.aliyun.com/repository/central</url>
    </mirror>
</mirrors>

这样,就能满足需求。

注意,可能在编辑器显示 pom.xml 是错误的,但是使用maven构建jar包没有问题。

2、安装 maven

Apache Maven 下载地址:http://maven.apache.org/download.cgi
官方安装说明: http://maven.apache.org/install.html

3. 插件

3.1 maven-shade-plugin

参考资料:

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

这个插件可以将依赖的jar打包到 1 个 jar 包,能够重命名某些依赖的包名。

重命名依赖的包名,可以避免类覆盖。
比如,A 包含了 v1 版本的 B 组件,C 包含了 v2 版本的 B 组件,当 C 引入 A 时,B组件会发生覆盖,如果 A 并不想被 C 覆盖那么可以在打包时将依赖的 B 组件的包名重命名。

系统要求

Plugin Version Maven JDK
3.5.2 3.2.6 8
3.5.1 3.2.5 8
from 3.4.0 to 3.5.0 3.1.1 8
3.3.0 3.1.1 7
from 3.2.0 to 3.2.4 3.0 7
from 3.0.0 to 3.1.1 3.0 6
from 2.0 to 2.4.3 3.0 5
from 1.5 to 1.7.1 2.0.6 5
up to 1.4 2.0.6 1.4

3.1.1 配置插件

插件 goal 只有 1 个 :shade 。是绑定在构建生命周期的 package 阶段。

配置示例:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

3.1.2 使用场景示例

官方资料

1 选择打包的依赖
使用到的标签:

  • <excludes> 排除
  • <includes> 包含
  • <minimizeJar> - 仅将依赖的类打包,依赖包中其他的类不打包。
  • <excludeDefaults>false<\excludeDefaults>

2 打包时调整依赖 class 所在的包

<relocation> 标签用于将某些依赖 jar 中的类移动到新包下,并调整影响到的字节码。

以下示例表明,除了org.codehaus.plexus.util.xml.Xpp3Dom 类和 org.codehaus.plexus.util.xml.pull 包 其他的类将会被放到 org.shaded.plexus.util 包下:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <relocations>
                <relocation>
                  <pattern>org.codehaus.plexus.util</pattern>
                  <shadedPattern>org.shaded.plexus.util</shadedPattern>
                  <excludes>
                    <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                    <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                  </excludes>
                </relocation>
              </relocations>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

使用 include 标签可以缩窄匹配范围
以下示例表明仅 org.codehaud.plexus.util.io 包下的类会被重新放到 org.codehaus.plexus.util.io 下:

<project>
  ...
   <relocation>
     <pattern>org.codehaus.plexus.util</pattern>
     <shadedPattern>org.shaded.plexus.util</shadedPattern>
     <includes>
       <include>org.codehaud.plexus.util.io.*</include>
     </includes>
   </relocation>
  ...
</project>

3 打包变形后的 Artifact

默认情况下,plugin 会使用 shaded artifact 替换项目的 artifact。如果都需要 install 或 deploy 到 repository,可以按如下配置,将 shaded artifact 作为二级 artifact :

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense -->
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

4 可执行 jar

创建可执行 jar,只需要指定 main class 即可,使用 ManifestResourceTransformer 在 MANIFEST.MF 添加 main class:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

以下示例中,ManifestResourceTransformer 用于设置 MANIFEST.MF 中的 Main-Class ,并添加其他条目:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
                    <!-- 在 MANIFEST.MF 添加 Build-Number 条目 -->
                    <Build-Number>123</Build-Number>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

** Resource Transformers**

org.apache.maven.plugins.shade.resource 中的 resource transformer:

ApacheLicenseResourceTransformer Prevents license duplication
ApacheNoticeResourceTransformer Prepares merged NOTICE
AppendingTransformer Adds content to a resource
ComponentsXmlResourceTransformer Aggregates Plexus components.xml
DontIncludeResourceTransformer Prevents inclusion of matching resources
GroovyResourceTransformer Merges Apache Groovy extends modules
IncludeResourceTransformer Adds files from the project
ManifestResourceTransformer Sets entries in the MANIFEST
PluginXmlResourceTransformer Aggregates Mavens plugin.xml
ResourceBundleAppendingTransformer Merges ResourceBundles
ServicesResourceTransformer Relocated class names in META-INF/services resources and merges them.
XmlAppendingTransformer Adds XML content to an XML resource

org.apache.maven.plugins.shade.resource.properties 中的 resource transformer(v3.2.2):

PropertiesTransformer Merges properties files owning an ordinal to solve conflicts
OpenWebBeansPropertiesTransformer Merges Apache OpenWebBeans configuration files
MicroprofileConfigTransformer Merges conflicting Microprofile Config properties based on an ordinal

3.2 maven-assembly-plugin

参考资料

The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files.

这个插件允许开发者,混合项目(包含 dependencies, modules, site documentation, and other files)输出到 1个单独的可发行的打包文件中。

posted on 2021-09-23 10:42  不安分的黑娃  阅读(26)  评论(0编辑  收藏  举报