我是Maven的新手,我想我已经开始了解它是如何工作的。但我无法理解maven程序集插件。我想要实现的是:
当打包所有项目及其各自的依赖项后,我希望将它们全部放在目标目录中。我不希望将它们打包到一个超级jar中,因为系统基于模块。
让我解释一下,我有一个主项目,服务器,在maven项目中“常见”,我有两个模块,“核心”和“android”。在公共文件夹中,还有一个我想复制的conf文件夹。我想要这个结构:
我的项目结构如下:
感谢您提供任何帮助,或以正确的方式提供指示。 :)
编辑这是100%工作的ant构建文件,也许我应该继续使用它?
<target name="init">
<mkdir dir="dist" />
<mkdir dir="dist/conf/" />
<mkdir dir="dist/modules/" />
<mkdir dir="dist/libs/" />
<copy includeemptydirs="false" todir="dist/conf">
<fileset dir="common/conf" />
</copy>
</target>
<target name="copy-server">
<copy todir="dist">
<fileset file="common/target/server*.jar" />
</copy>
</target>
<target name="copy-modules">
<copy todir="dist/modules/">
<fileset file="core/target/*.jar" />
<fileset file="android/target/*.jar" />
</copy>
</target>
<target name="copy-libs">
<copy todir="dist/libs">
<fileset dir="common/target/libs" />
<fileset dir="core/target/libs" />
<fileset dir="android/target/libs" />
</copy>
<delete>
<fileset file="dist/libs/server*.jar" />
</delete>
</target>
<target name="clean">
<delete dir="dist" />
</target>
<target name="full-build" depends="clean, init, copy-server, copy-libs, copy-modules, increment">
<echo message="Copying the fully built Maven project" />
</target>
<target name="increment">
<propertyfile file="common/conf/version.properties">
<entry key="build.number" type="int" operation="+" default="0" />
</propertyfile>
<property file="common/conf/version.properties" />
<echo message="Build number is ${build.number}"/>
</target>
答案 0 :(得分:4)
首先,程序集插件的作用是:它使得包含Maven项目的工件,依赖项和其他相关文件的tar或zip存档变得非常容易。
听起来你需要做的只是set up the assembly plugin带有自定义描述符来引入你感兴趣的jar文件和配置文件。如果你有一个代表“可分发”东西的单个模块 - 即使该模块依赖于其他模块 - 那么您可能只需向其中添加assembly descriptor,其中包含一些files,fileSets和/或dependencySets。< / p>
如果项目中有多个模块要包含,并且没有任何模块依赖于所有模块,那么您需要查看moduleSets。我从来没有必须自己使用它们,但它们正是出于这个问题。
答案 1 :(得分:2)
此设置将完全按照您的意愿解决。
directory layout
+- pom.xml
+- android
| +- pom.xml
| +- src
| +- main
| +- java
+- core
| +- pom.xml
| +- src
| +- main
| +- java
+- common
+- pom.xml
+- src
+- main
+- java
+- resources
+- conf
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<modules>
<module>android</module>
<module>common</module>
<module>core</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
android/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
</project>
core/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
</project>
common/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最后一个pom是您可以根据需要改变行为的地方。
如果你想把所有这些包装在一些超级罐中,那么你可以自由地这样做。
修改强>:
好的,在阅读你的评论并查看你的Ant构建脚本之后,我进行了以下设计/设置,它可以为你提供更多或更少的内容。
directory layout
+- pom.xml
+- android
| +- pom.xml
| +- src
| +- main
| +- java
+- core
| +- pom.xml
| +- src
| +- main
| +- java
+- common
| +- pom.xml
| +- conf.xml
| +- src
| +- main
| +- java
| +- resources
| +- conf
+- dist
+- pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<modules>
<module>android</module>
<module>common</module>
<module>core</module>
<module>dist</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
<classifier>conf</classifier>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
android/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
</project>
core/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
</project>
common/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}-${project.version}</name>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>conf.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
common/conf.xml
这个程序集描述符会将你的conf文件打包到一个单独的jar中,任何项目都可以依赖它。
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>conf</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/resources/conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.properties</include>
</includes>
</fileSet>
</fileSets>
</assembly>
dist/pom.xml
dist模块将解压缩conf依赖项并将其他依赖项复制到目标目录。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>dist</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}-${project.version}</name>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<classifier>conf</classifier>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>modules</id>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.example</groupId>
<artifactId>android</artifactId>
</artifactItem>
<artifactItem>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
</artifactItem>
<artifactItem>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<classifier>conf</classifier>
</artifactItem>
</artifactItems>
<excludes>**/MANIFEST.MF</excludes>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我倾向于喜欢使用maven-dependency-plugin
,我发现它非常强大。
dist模块将具有对其他模块的所有必要依赖性,并根据需要下载和解压缩。如果您希望将其全部打包为zip或tar.gz或其他格式,那么您可以使用maven-assembly-plugin
。
答案 2 :(得分:0)
在为项目构建时,Maven将默认为以下结构:
但是,一切都不会丢失!与maven中的大多数选项一样,使用正确的配置标记可以轻松覆盖它。在您的情况下,您希望将标记(在每个模块POMS中)下的outputDirectory标记设置为正确的目录。
例如,应编辑$ {basedir} /common/pom.xml以添加此行:
...
<build>
<outputDirectory>../target</outputDirectory>
...
您的./android/pom.xml将进行类似编辑:
...
<build>
<outputDirectory>../target/modules</outputDirectory>
...
我不确定复制配置文件的最佳方法。您可能希望尝试查找(或编写,因为它是一个相当简单的操作)插件,只需复制文件并为其指定范围,例如“编译”,同时将其指向正确的目录。