我不明白maven。更好地使用ant,但是......我已经设法创建了jar(有或没有依赖),我已经设法将bat runner脚本复制到jar附近,但现在我想用这个jar和这个bat创建zip。所以我使用程序集插件并获取BUUUM !!!! CADAAAM!在我的配置中,它发生了,它与jar包装并行执行。我写了汇编文件:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jg</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/123</outputDirectory>
<excludes>
<exclude>assembly/**</exclude>
<exclude>runners/**</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
然后,我绑定了maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<inherited>false</inherited>
<configuration>
<archive>
<manifest>
<mainClass>by.dev.madhead.lzwj.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly/assembly.xml</descriptor>
<!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
现在我在./target
:
第三个让我感到愤怒。它包含: 123目录包含:
如你所见,我得到解压缩依赖项的jar,排除DIRS !!!!和dir 123,这实际上就是我想要的(哦!汇编插件就是这样!!!)。
我想获得带有依赖关系的jar并使用classpath更正清单。作为一个选项,我希望jar具有解压缩的依赖关系(我在程序集中了解<unpack>false</unpack>
,但无法使其工作)。我想将/ 123更改为/并获取NORMAL JAR而不排除文件!我想要两个单独的任务来构建jar和zip(它是用maven中的配置文件完成的吗?)在ant中,我会写这样的东西:
<target name="jar_with_deps" depends-on="compile">
<jar>
here i copy classes (excluding some dirs with runner script), and build manifest
</jar>
<copy>
copy bat file from src/main/resources/runner/runner.bat
</copy>
</target>
<target name="zip" depends-on="jar_with_deps">
<zip>
Get jar from previous target, get runner.bat. Put them in zip
</zip>
</target>
不好意思,如果我过于表达,但我对这种隐含行为感到非常生气。我真的很困惑。
答案 0 :(得分:6)
为了防止其他人,我发现这很容易做到,至少对我的基本需求而言。我已经在使用Maven Shade插件来构建一个包含所有依赖项的jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<configuration></configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
所以当我运行mvn package
时,它会生成target / MyApp-version.jar,而我想要一个包含MyApp-version.jar的MyApp-version.zip以及其他一些文件(自述文件等)。 )。所以,我添加了Assembly插件:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
上述块指的是assembly.xml
,它配置the way the plugin works:
<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>release</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<includes>
<include>MyApp-${app.version}.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>CHANGES.md</source>
<fileMode>0644</fileMode>
</file>
<file>
<source>LICENSE</source>
<fileMode>0644</fileMode>
</file>
<file>
<source>README</source>
<fileMode>0644</fileMode>
</file>
</files>
</assembly>
(${app.version}
在pom.xml <properties>
元素中定义。)
就是这样,现在mvn package
生成了jar和zip。
答案 1 :(得分:1)
您必须选择实现目标:
根据项目的大小和结构,我会选择安全的方式:选项2.保证这一项具有正确的构建和dep命令。选项1在某种程度上违反了maven方式。
答案 2 :(得分:1)
我在pom.xml中创建了两个配置文件:
<profiles>
<profile>
<id>jar-with-dependencies</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<archive>
<manifest>
<mainClass>by.dev.madhead.lzwj.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>distro</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/distro.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
现在我可以创建简单的jar(mvn clean package
),带有依赖项的jar(mvn clean package -Pjar-with-dependencies
)。我也可以致电mvn package -Pdistro
来创建zip。但我需要在手动之前使用-Pjar-with-dependencies调用maven。除此之外,一切都还可以。