我在maven中有一个多模块项目,它使用JAXB生成的源:
parent
module A
module B (depends on module A)
没有JAXB,一切都很好。当我将JAXB插件添加到模块B时,maven抱怨:
Failed to execute goal on project moduleB: Could not resolve dependencies for
project groupId:A:jar:1.7.0: Could not find artifact groupId:A:jar:1.7.0:
in thirdparty (http://10.0.0.2:8081/nexus/content/repositories/thirdparty)
据我所知,这是因为jaxb maven插件要求在生成源阶段,在编译模块A之前解析所有依赖关系。我们没有在我们的本地存储库中安装模块A的机制 - 我们从不需要它,因为两个模块都在同一个git存储库中。
我发现的丑陋解决方案是:
parent
module A
module B (depends on module A & C)
module C (contains only JAXB generate-sources)
JAXB不需要模块A,因此所有内容都会编译。这个解决方案确实有效,但很难看。通常模块包含有用的代码,但是为了将有用的代码放在模块C中,我必须使它依赖于模块A而导致相同的错误。 (切换到不同的JAXB插件不起作用,因为它还在生成源阶段解决了依赖关系。)
问题:有没有办法在不创建另一个模块的情况下避免这种依赖?
(可能的解决方案可能涉及为模块B添加阶段或范围,更改模块B需要模块A的依赖关系的范围,因此在生成源之后或创建排除时不需要它,因此JAXB不会寻找这种特殊的依赖。)
以下所有内容都是pom.xml文件的极简主义版本。它来自工作的poms,但我不是100%确定它会以现在的形式编译。
<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>
<artifactId>artFacts</artifactId>
<groupId>com.group</groupId>
<version>1.7.0</verison>
<repositories>
<repository>
<id>thirdparty</id>
<url>http://10.0.0.2:8081/nexus/content/repositories/thirdparty</url>
</repository>
</repositories>
<modules>
<module>modA</module>
<module>modB</module>
</modules>
</project>
<project>
<parent>
<artifactId>artFacts</artifactId>
<groupId>com.group</groupId>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>modA</artifactId>
<packaging>jar</packaging>
</project>
<project>
<parent>
<artifactId>artFacts</artifactId>
<groupId>com.group</groupId>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>modB</artifactId>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>xjc_raw</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.group.jaxb</packageName>
<schemaDirectory>${basedir}/xsd/</schemaDirectory>
<outputDirectory>${project.build.directory}/generated-sources/jaxb</outputDirectory>
<staleFile>${project.build.directory}/generated-sources/jaxb/.staleFlag</staleFile>
<target>2.1</target>
</configuration>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<!-- Tell Eclipse where to find the generated sources. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/jaxb</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- This is the dependency which fails because it can't be resolved
during the generate-sources phase. -->
<dependency>
<groupId>com.group</groupId>
<artifactId>modA</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependencies>
</project>
在此表单中,项目将失败并显示上面的错误消息。将此更改为丑陋的解决方案可以通过创建modC并将JAXB的所有内容放在那里来实现。在此之后,只需要使modB依赖于modA和modC,再加上modC到父级。