我有一个多模块项目,其中一些模块有多个源目录(一个用于手工创建的代码,另一个用于生成的代码)。
项目如下:
Product
+-- pom.xml
+-- MagniCompCommon
+-- src/main/java
+-- src-gen
+-- pom.xml
我使用build-helper-maven-plugin添加src-gen目录。但是,当我在父maven中通过“mvn clean install”进行编译时,它不会在src-gen中编译任何内容,也不会在尝试编译src / main / java文件时将其包含在类路径中。由于src/main/java
中的许多java代码都引用了src-gen代码,因此编译失败。
这是Parent pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.magnicomp</groupId>
<artifactId>Product</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>MagniCompCommon</module>
<module>Model</module>
<module>Common</module>
<!-- <module>Agent</module> -->
<!-- <module>Doc</module> -->
</modules>
<properties>
<!-- MagniComp common -->
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.3.11.Final</hibernate.version> <!-- was 4.3.10.Final -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Product specific -->
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -->
<jersey.glassfish.version>2.21</jersey.glassfish.version>
<javax.servlet.version>3.0</javax.servlet.version>
<bouncy.version>1.51</bouncy.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src-gen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Use Java 8 (default is 5) -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
我也尝试将${project.basedir}/src-gen
用于<source>
,但这没有任何区别。
这是MagniCompCommon pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.magnicomp</groupId>
<artifactId>Product</artifactId>
<version>1.0</version>
</parent>
<artifactId>MagniCompCommon</artifactId>
<packaging>jar</packaging>
.... snip ...
答案 0 :(得分:1)
转移到孩子:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src-gen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
答案 1 :(得分:0)
父POM
<packaging>pom</packaging>
这意味着这个POM没有正常的构建生命周期,那里没有代码。
将build-helper-maven-plugin
描述从父项目移至MagniCompCommon
的POM,build.plugins
和不 build.pluginmanagement.plugins
。
修改:此外,如果您未将src-gen
签入源代码管理中(并且因为它已生成,最好不要),请改用target/generated-sources
,然后再使用build-helper-maven-plugin
必要的。
答案 2 :(得分:0)
以下是MagniCompCommon pom.xml的工作版本:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src-gen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
答案 3 :(得分:0)
创建了多模块maven项目(与您的相同),并将以下构建标记添加到子模块MagniCompCommon pom.xml文件中。没有编译错误,我工作得很好
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>src/main/java/**/*.java</include>
<include>src-gen/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>`