【问题标题】:Maven multi-module project with license plugin带有许可证插件的 Maven 多模块项目
【发布时间】:2012-12-27 16:59:18
【问题描述】:

我有一个多模块project,我正在尝试设置许可证插件来管理所有许可证。这是项目设置:

─── transfuse-project
    ├── examples
    │   ├── helloAndroid
    │   │   ├── pom.xml
    │   │   ├── ...
    │   ├── integrationTest
    │   │   ├── pom.xml
    │   │   ├── ...
    │   ├── pom.xml
    │   └── ...
    ├── transfuse
    │   ├── pom.xml
    │   ├── ...
    ├── transfuse-api
    │   ├── pom.xml
    │   ├── ...
    ├── NOTICE
    └── pom.xml

每个 pom.xml 都继承自 transfuse-project pom.xml。在项目 pom.xml 中,我设置了许可证插件以将 NOTICE 应用于相关文件:

        <plugin>
            <groupId>com.mycila.maven-license-plugin</groupId>
            <artifactId>maven-license-plugin</artifactId>
            <version>1.9.0</version>
            <configuration>
                <header>NOTICE</header>
                <includes>
                    <include>**/*.java</include>
                    <include>**/*.xml</include>
                </includes>
                <excludes>
                    <exclude>**/.*/**</exclude>
                    <exclude>target/**</exclude>
                    <exclude>**/AndroidManifest.xml</exclude>
                </excludes>
                <properties>
                    <year>2013</year>
                    <name>John Ericksen</name>
                </properties>
                <useDefaultExcludes>true</useDefaultExcludes>
                <strictCheck>true</strictCheck>
            </configuration>
            <executions>
                <execution>
                    <id>check-headers</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

如果我直接从根 (transfuse-project) 构建,则此配置有效。当我直接构建 integrationTest 示例或 api 时出现问题。 Maven在项目根目录找不到我提供的NOTICE文件:

[ERROR] Failed to execute goal com.mycila.maven-license-plugin:maven-license-plugin:1.9.0:check (check-headers) on project transfuse-api: Some files do not have the expected license header -> [Help 1]

更糟糕的是,它找到了另一个依赖项的 NOTICE 文件。如果我在子模块中运行mvn license:format,它会将所有模块的头文件替换为依赖项的 NOTICE 文件。

我相信我可以在每个子模块中添加一个 NOTICE 文件来解决这个问题,并为每个子模块 pom 配置它自己的许可证插件,但我想尽可能避免这种重复。是否有一些配置或设置适用于我的项目设置?

【问题讨论】:

    标签: maven pom.xml multi-module project-structure


    【解决方案1】:

    尝试通过使用绝对路径

    <header>${basedir}/NOTICE</header>
    

    如果这不起作用,请尝试set a property to and in the parent module's basedir 并使用它:

    <header>${main.basedir}/NOTICE</header>
    

    第三种选择是在运行时设置属性:

    mvn clean install -Dmain.basedir=path/to/main/basedir
    

    编辑:

    好的,另一种选择是在您的许可证插件之前执行maven-dependency-plugin。但你必须确保家长附上通知(maven-assembly-plugin plugin)

    <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-dependency-plugin</artifactId>
             <version>2.6</version>
             <executions>
               <execution>
                 <id>unpack-parent</id>
                 <phase>verify</phase>
                 <goals>
                   <goal>unpack</goal>
                 </goals>
                 <configuration>
                   <artifactItems>
                     <artifactItem>
                       <groupId>parent</groupId>
                       <artifactId>parent</artifactId>
                       <version>parent</version>
                       <type>pom</type>
                       <overWrite>false</overWrite>
                       <outputDirectory>${project.build.directory}/license</outputDirectory>
                       <includes>NOTICE</includes>
                     </artifactItem>
                   </artifactItems>
                 </configuration>
               </execution>
             </executions>
           </plugin>
    

    header 随即更改:

    <header>${project.build.directory}/license/NOTICE</header>
    

    编辑2:

    我遇到了 find-maven-plugin。我认为这可行:

    <plugin>
        <groupId>com.github.goldin</groupId>
        <artifactId>find-maven-plugin</artifactId>
        <version>0.2.5</version>
        <executions>
            <execution>
                <id>find-notice-file</id>
                <goals>
                    <goal>find</goal>
                </goals>
                <phase>validate</phase>
                <configuration>
                    <propertyName>notice.file</propertyName>
                    <file>NOTICE</file>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

    • 啊,我也试过了。从根目录构建是可行的,但构建任何子模块都会失败。
    • 说得太早了。这: ${project.parent.parent.basedir} 从根目录构建时似乎没有引用祖父母。想法?
    • 我是将依赖插件添加到根模块还是每个子模块?
    • 在根目录下,但执行应该在子pom中
    • 呃,这个解决方案在 Maven 中遇到了一个错误:“[错误] 无法执行目标 org.apache.maven.plugins:maven-dependency-plugin:2.6:unpack (unpack-parent)在项目 transfuse-project 上:未知的存档器类型:没有这样的存档器:'xml'。"
    【解决方案2】:

    使用这两个配置属性集尝试您的示例:

    <plugin>
      <groupId>com.mycila</groupId>
      <artifactId>license-maven-plugin</artifactId>
      <version>2.7</version>
      <configuration>
        <header>/NOTICE</header>
        <aggregate>true</aggregate>
      </configuration>
    </plugin>
    

    【讨论】:

      【解决方案3】:

      实际上有一个更简单的方法可以做到这一点。只需在配置中将aggregate 标签设置为true。这是完整的maven-license-plugin 定义:

      <plugin>
          <groupId>com.mycila.maven-license-plugin</groupId>
          <artifactId>maven-license-plugin</artifactId>
          <version>1.10.b1</version>
          <configuration>
              <header>etc/header.txt</header>
              <aggregate>true</aggregate>
              <includes>
                  <include>**/*.java</include>
              </includes>
          </configuration>
      </plugin>
      

      然后你只需要在父根目录的etc/header.txt 中有一个名为header.txt 的文件。

      【讨论】:

      • 这在构建父模块时有效,但在您单独构建单个模块时无效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2011-09-03
      相关资源
      最近更新 更多