Maven - POM 设计更多模块与独特的 libs 模块
Maven - POM design for more module with unique libs module
我想为 Java 标准项目配置 Maven POM,我想设计以下模块架构:
Parent
项目: 该项目构建所有子模块。
JAR
项目: 这个项目创建一个最终的 JAR 文件。
Core
项目:这是一个主项目,但是会有类似的项目。
Libs
project: 这是一个包含所有库依赖的项目,将被各种主要模块(如Core
)使用.
当我引用我在 Project-Libs
的依赖项中定义的库时,编译核心模块时遇到问题。
(在下面的特定示例中,当我在 Core
class 测试中导入 JUnit 库时出现编译错误)。
我配置的Maven POM如下:
Parent 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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>../Project-Libs</module>
<module>../Project-Core</module>
<module>../Project-JAR</module>
</modules>
Project-Libs
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-Libs</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-Core</artifactId>
<packaging>jar</packaging>
<name>Project-Core</name>
<dependencies>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
Project-JAR
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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-JAR</artifactId>
<packaging>jar</packaging>
<name>Project-JAR</name>
<dependencies>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Core</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a JDK compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<module>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
</module>
<module>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Core</artifactId>
</module>
</modules>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
拜托,你能帮我找出错误吗?非常感谢。
我会放弃这个 Project-Libs
项目,因为 apparently 它只是用来保存您其他项目的 <dependency>
设置。这也可以在 parent Project
POM 中完成。通过将这些依赖项外包给单独的 POM,您会错过 parent POM 设计为您所做的事情:传递 POM 设置,例如<dependencies>
,到 child 个项目。这种额外的 Project-Libs
项目会不必要地污染您的所有其他 POM,其 <dependencies>
设置。
我会像这样设计 parent Project
POM:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
...
</dependencies>
...
从某种角度来看,在 Project-Libs
中声明的依赖项未被您的其他项目找到也合乎逻辑。 Project-Libs
不是 parent,而是对它们的依赖。 Maven 可以很好地处理传递依赖性,但我也开始了解它试图尽可能高效。现在,由于 <packaging>pom
类型的项目不包含任何要编译、测试、打包、安装和部署的代码,因此可能会简单地忽略此类项目中的依赖项,而不管引入的任何传递性兄弟项目。
您还可以将 BOM (bill of material) POM 作为 parent 引入您的 Project
parent。
您应该在父 pom.xml 文件中使用 DependecyManagement。
我想为 Java 标准项目配置 Maven POM,我想设计以下模块架构:
Parent
项目: 该项目构建所有子模块。JAR
项目: 这个项目创建一个最终的 JAR 文件。Core
项目:这是一个主项目,但是会有类似的项目。Libs
project: 这是一个包含所有库依赖的项目,将被各种主要模块(如Core
)使用.
当我引用我在 Project-Libs
的依赖项中定义的库时,编译核心模块时遇到问题。
(在下面的特定示例中,当我在 Core
class 测试中导入 JUnit 库时出现编译错误)。
我配置的Maven POM如下:
Parent 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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>../Project-Libs</module>
<module>../Project-Core</module>
<module>../Project-JAR</module>
</modules>
Project-Libs
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-Libs</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-Core</artifactId>
<packaging>jar</packaging>
<name>Project-Core</name>
<dependencies>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
Project-JAR
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.Whosebug</groupId>
<artifactId>Project</artifactId>
<version>1.0.0</version>
<relativePath>../Project</relativePath>
</parent>
<artifactId>Project-JAR</artifactId>
<packaging>jar</packaging>
<name>Project-JAR</name>
<dependencies>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Core</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a JDK compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<module>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Libs</artifactId>
</module>
<module>
<groupId>com.Whosebug</groupId>
<artifactId>Project-Core</artifactId>
</module>
</modules>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
拜托,你能帮我找出错误吗?非常感谢。
我会放弃这个 Project-Libs
项目,因为 apparently 它只是用来保存您其他项目的 <dependency>
设置。这也可以在 parent Project
POM 中完成。通过将这些依赖项外包给单独的 POM,您会错过 parent POM 设计为您所做的事情:传递 POM 设置,例如<dependencies>
,到 child 个项目。这种额外的 Project-Libs
项目会不必要地污染您的所有其他 POM,其 <dependencies>
设置。
我会像这样设计 parent Project
POM:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
...
</dependencies>
...
从某种角度来看,在 Project-Libs
中声明的依赖项未被您的其他项目找到也合乎逻辑。 Project-Libs
不是 parent,而是对它们的依赖。 Maven 可以很好地处理传递依赖性,但我也开始了解它试图尽可能高效。现在,由于 <packaging>pom
类型的项目不包含任何要编译、测试、打包、安装和部署的代码,因此可能会简单地忽略此类项目中的依赖项,而不管引入的任何传递性兄弟项目。
您还可以将 BOM (bill of material) POM 作为 parent 引入您的 Project
parent。
您应该在父 pom.xml 文件中使用 DependecyManagement。