写在前面
比如编写javaagent程序时,或者是生成可执行jar包时都需要配置manifest清单文件,本文看下通过maven插件如何来做。
1:配置(不包含源码class)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<finalName>agentAndAsmPrintParamAndWasteTime-v${version}</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestEntries>
<Premain-Class>com.dahuyou.agentandasm.agent.PreMain</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在<manifestEntries>
中配置你需要的其他配置就行,如下运行:
2:配置(包含源码class和依赖)
想要实现这种效果,我们需要自己来定义build.xml,这里参考jar-with-dependendy的xml,如下:
可以看到这里的id也就是我们在pom xml中配置的,如下:
我们直接在jar-with-dependency的基础上增加拷贝项目class的内容,如下:
<?xml version='1.0' encoding='UTF-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: jar-with-dependencies -->
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 https://maven.apache.org/xsd/assembly-2.2.0.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies-mine</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<!--复制resources下的文件到jar包的conf目录-->
<!--<directory>src/main/resources/META-INF</directory>
<outputDirectory>META-INF</outputDirectory>-->
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
<!-- END SNIPPET: jar-with-dependencies -->
这里我们只是在原有基础上增加了拷贝源码class的操作,接着修改pom插件配置,使用自定义的build xml,如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<finalName>change-method-param-val-${version}</finalName>
<!--
<descriptorRefs>
<descriptorRef>bin</descriptorRef>
</descriptorRefs>
-->
<descriptors>jar-with-dependencies-and-sourceclasses.xml</descriptors>
<archive>
<manifestEntries>
<Premain-Class>com.dahuyou.change.method.param.MyPreMain</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<descriptors>jar-with-dependencies-and-sourceclasses.xml</descriptors>
即为设置使用。注意一定要先compile,避免没有生成classes,接着打包就和常规方式一样了,可以看下最终生成的jar包就包含源码class了,如下: