如何同时打包成jar和tar?
时间: 2024-01-21 07:20:02 浏览: 73
假设你使用的是Maven构建工具,可以通过配置Maven插件来实现同时打包成jar和tar。
在pom.xml文件中添加以下插件配置:
```
<build>
<plugins>
<!-- 打包jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包tar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
然后在项目根目录下创建src/main/assembly/assembly.xml文件,添加以下内容:
```
<assembly>
<id>tar</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
```
以上配置会在Maven打包时同时生成jar和tar包。可以通过执行以下命令进行打包:
```
mvn clean package
```
执行完毕后,会在target目录下生成两个文件:xxx.jar和xxx.tar.gz。
阅读全文