maven项目设置manifest清单文件插件配置

写在前面

比如编写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了,如下:
在这里插入图片描述
在这里插入图片描述

写在后面

参考文章列表

maven-assembly-plugin插件使用详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值