maven 常用插件

maven-shade-plugin

  1. 将依赖的jar包打包到当前jar包(常规打包是不会将所依赖jar包打进来的);
  2. 对依赖的jar包进行重命名(用于类的隔离);
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <id>shade-flink</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadeTestJar>true</shadeTestJar>
                <shadedArtifactAttached>false</shadedArtifactAttached>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <dependencyReducedPomLocation>${project.basedir}/target/dependency-reduced-pom.xml
                </dependencyReducedPomLocation>
                <!-- Filters MUST be appended; merging filters does not work properly, see MSHADE-305 -->
                <filters combine.children="append">
                    <!-- Globally exclude log4j.properties from our JAR files. -->
                    <filter>
                        <artifact>*</artifact>
                        <excludes>
                            <exclude>log4j.properties</exclude>
                            <exclude>log4j2.properties</exclude>
                            <exclude>log4j-test.properties</exclude>
                            <exclude>log4j2-test.properties</exclude>
                        </excludes>
                    </filter>
                    <!-- drop entries into META-INF and NOTICE files for the dummy artifact -->
                    <filter>
                        <artifact>org.apache.flink:force-shading</artifact>
                        <excludes>
                            <exclude>**</exclude>
                        </excludes>
                    </filter>
                    <!-- io.netty:netty brings its own LICENSE.txt which we don't need -->
                    <filter>
                        <artifact>io.netty:netty</artifact>
                        <excludes>
                            <exclude>META-INF/LICENSE.txt</exclude>
                        </excludes>
                    </filter>
                </filters>
                <artifactSet>
                    <includes>
                        <!-- Unfortunately, the next line is necessary for now to force the execution
                        of the Shade plugin upon all sub modules. This will generate effective poms,
                        i.e. poms which do not contain properties which are derived from this root pom.
                        In particular, the Scala version properties are defined in the root pom and without
                        shading, the root pom would have to be Scala suffixed and thereby all other modules.
                        -->
                        <include>org.apache.flink:force-shading</include>
                    </includes>
                </artifactSet>
                <transformers combine.children="append">
                    <!-- The service transformer is needed to merge META-INF/services files -->
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <!-- The ApacheNoticeResourceTransformer collects and aggregates NOTICE files -->
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
                        <projectName>Apache Flink</projectName>
                        <encoding>UTF-8</encoding>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

exec-maven-plugin

可以用这个插件执行java代码或命令

  1. exec:exec 执行单独或java进程
  2. exec:java 在jvm中执行java进程
 <build>
        <plugins>
        <!-- http://www.mojohaus.org/exec-maven-plugin/ -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>cn.freemethod.plugin.App</mainClass>
                            <arguments>
                                <argument>arg0</argument>
                                <argument>arg1</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

maven-enforcer-plugin

解决maven依赖包冲突

我们会经常碰到这样的问题,在pom中引入了一个jar,里面默认依赖了其他的jar包。jar包一多的时候,我们很难确认哪些jar是我们需要的,哪些jar是冲突的。此时会出现很多莫名其妙的问题,什么类找不到啦,方法找不到啦,这种可能的原因就是jar的版本不是我们所设想的版本,但是我们也不知道低版本的jar是从哪个maven里面引用的。此时我们的maven-enforcer-plugin就可以登场了。

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce</id>
            <configuration>
              <rules>
                <dependencyConvergence/>
              </rules>
            </configuration>
            <goals>
              <goal>enforce</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

在进行mvn clean package的时候,会在console中打印出来冲突的jar版本和其父pom,如下:


Dependency convergence error for org.slf4j:slf4j-api1.6.1 paths to dependency are:
 
[ERROR]
Dependency convergence error for org.slf4j:slf4j-api:1.6.1 paths to dependency are:
+-org.myorg:my-project:1.0.0-SNAPSHOT
  +-org.slf4j:slf4j-jdk14:1.6.1
    +-org.slf4j:slf4j-api:1.6.1
and
+-org.myorg:my-project:1.0.0-SNAPSHOT
  +-org.slf4j:slf4j-nop:1.6.0

这个时候,我们看一眼就知道应该把那个dependency中的哪个jar进行exclude。

maven-bundle-plugin

该插件可以把项目依赖的jar包打包到项目的根目录,前提是必须加上必须加上

*;scope=compile|runtime;inline=false这个配置

<plugin>
	<groupId>org.apache.felix</groupId>
	<artifactId>maven-bundle-plugin</artifactId>
	<version>3.2.0</version>
	<extensions>true</extensions>
	<configuration>
		<instructions>
			<Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>
		</instructions>
		<archive>
			<manifestEntries>
				<Bundle-ManifestVersion>2</Bundle-ManifestVersion>
				<Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name>
				<Bundle-SymbolicName>${project.groupId}.${project.artifactId}
				</Bundle-SymbolicName>
				<Bundle-Version>${project.version}</Bundle-Version>
				<Bundle-Vendor>${project.groupId}</Bundle-Vendor>
				<Bundle-Activator>com.ht.pojo.Activator</Bundle-Activator>
				<Export-Package>com.ht.pojo</Export-Package>
				<Import-Package>org.osgi.framework</Import-Package>
			</manifestEntries>
		</archive>
	</configuration>
</plugin>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值