【问题标题】:Using MOJO build-helper-maven-plugin to add more source folders to MAVEN使用 MOJO build-helper-maven-plugin 向 MAVEN 添加更多源文件夹
【发布时间】:2013-10-24 08:56:38
【问题描述】:

我对如何向 MAVEN 添加更多源文件夹进行了更多讨论,我选择使用 MOJO 的 build-helper-maven-plugin 插件。 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>my.maven.tests</groupId>
  <artifactId>helper</artifactId>
  <version>1.0</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>add-gen-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
               <source>src-gen/gen/java</source>
              </sources>
            </configuration>
          </execution>
          <execution>
            <id>add-extra-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/extra/java</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
          <includes>
            <include>src/main/java/**/*.java</include>
            <include>src-gen/gen/java/**/*.java</include>
            <include>src/extra/java/**/*.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

使用命令mvn clean compile 构建完成正常,没有错误,但没有生成类。

我确定我做错了什么,但我无法弄清楚。

【问题讨论】:

    标签: maven plugins compilation helper


    【解决方案1】:

    问题是你的 maven-compiler-plugin 的 includes 配置。 maven-compiler-plugin 将自动获取项目中配置的所有源文件夹 - 您无需通过 includes 标签定义它们。

    因此,在您的情况下,它将自动选择 src/main/java(标准 maven 源位置)以及您已配置要添加的 build-helper-maven-plugin 的两个 src-gen/gen/javasrc/extra/java

    您需要做的只是删除includes 部分,您的构建应该可以工作。所以你的 pom 中的 maven-compiler-plugin 只是:

    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
    ...
    

    【讨论】:

      【解决方案2】:

      另外,请注意源文件夹需要遵循 maven 文件结构约定(即,如果您的源文件夹直接包含您的 .java 文件,这将不起作用:它需要包含一个主文件夹和/或测试文件夹)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        相关资源
        最近更新 更多