Java Maven原生插件中的滑稽样板

Java Maven原生插件中的滑稽样板,java,maven,java-native-interface,native,boilerplate,Java,Maven,Java Native Interface,Native,Boilerplate,我目前正在重写我的OSS项目,使其尽可能易于开发人员和最终用户使用 然而,作为maven本机设置的一部分,对于我要创建的每个本机二进制文件,我似乎需要一个单独的pom.xml。这些文件中的绝大多数实际上是跨平台的相同文件 如何减少本机生成文件的样板文件?,例如: (除了输出文件的名称、编译器标志和javah的目标平台之外,几乎所有内容都在操作系统目标之间共享) 其他一些较小但相关的问题: 我只想分发捆绑的jar,如何关闭jnilib(etc)部署,而是使用默认的分类器部署jar(我可以

我目前正在重写我的OSS项目,使其尽可能易于开发人员和最终用户使用

然而,作为maven本机设置的一部分,对于我要创建的每个本机二进制文件,我似乎需要一个单独的
pom.xml
。这些文件中的绝大多数实际上是跨平台的相同文件

如何减少本机生成文件的样板文件?,例如:

(除了输出文件的名称、编译器标志和javah的目标平台之外,几乎所有内容都在操作系统目标之间共享)

其他一些较小但相关的问题:

  • 我只想分发捆绑的jar,如何关闭
    jnilib
    (etc)部署,而是使用默认的
    分类器部署
    jar
    (我可以创建一个
    本机的
    分类器jar,但这对于最终用户来说很难包含在内)
  • 显然,我永远无法一次性构建整个项目(因为有几个操作系统本机目标)。然而,Maven坚持尝试编译模块化的东西。我如何设置依赖关系树/工作流,以便最终用户只需要依赖一个项目,该项目将所有本机jar都包含在内

    • maven的继承特性可以大大减少样板代码。 为了简单起见,我省略了javah和其他JNI内容,只创建了两个C项目和一个公共父项目。 目录结构如下所示:

      +-NativeParent
      ! !
      ! +-src/main/native
      ! +-pom.xml
      +-NativeTest1
      ! !
      ! +-pom.xml
      +-NativeTest2
        !
        +-pom.xml
      
      NativeParent包含所有源代码,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>test</groupId>
         <artifactId>nativeParent</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <packaging>pom</packaging>
      
          <name>parent-pom</name>
      
          <modules>
              <module>../NativeTest1</module>
              <module>../NativeTest2</module>
          </modules>
      
          <dependencies>
          </dependencies>
      
      
      
         <build>
           <plugins>
             <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>native-maven-plugin</artifactId>
               <extensions>true</extensions>
               <configuration>
      
                 <sources>
                   <source>
                     <directory>../NativeParent/src/main/native</directory> 
                     <fileNames>
                       <fileName>krbwinclient.c</fileName>
                     </fileNames>
                   </source>          
                 </sources>
      
                 <linkerStartOptions>
                   <linkerStartOption>-Wl,--kill-at</linkerStartOption>
                   <linkerStartOption>-shared</linkerStartOption>
                 </linkerStartOptions>
                 <linkerEndOptions>
                  <linkerEndOption>-lSecur32</linkerEndOption>
                  <linkerEndOption>-lOle32</linkerEndOption>
                 </linkerEndOptions>
               </configuration>
               </plugin>
      
               </plugins>
          </build>
      
      </project>
      
      
      4.0.0
      测试
      本地父母
      0.0.1-快照
      聚甲醛
      母体聚甲醛
      ../NativeTest1
      ../NativeTest2
      org.codehaus.mojo
      原生maven插件
      真的
      ../NativeParent/src/main/native
      krbwincient.c
      -Wl,--杀死
      -共享
      -lSecur32
      -lOle32
      
      NativeTest1和NativeTest2的pom可以非常精简,因为它们只需定义不同于父pom的属性

      本地测试1:

      <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>test</groupId>
             <artifactId>nativeParent</artifactId>
             <version>0.0.1-SNAPSHOT</version>
          </parent>
      
         <groupId>test</groupId>
         <artifactId>native1</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <packaging>so</packaging>
      
         <build>
          <finalName>native1.so</finalName>
           <plugins>
             <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>native-maven-plugin</artifactId>
               <extensions>true</extensions>
               <configuration>
                 <compilerStartOptions>
                   <compilerStartOption>-Wl,--add-stdcall-alias</compilerStartOption>
                   <compilerStartOption>-DBUILD_SO</compilerStartOption>
                 </compilerStartOptions>
      
               </configuration>
               </plugin>
      
               </plugins>
          </build>
      
      </project>
      
      
      4.0.0
      测试
      本地父母
      0.0.1-快照
      测试
      本地1
      0.0.1-快照
      所以
      土生土长的
      org.codehaus.mojo
      原生maven插件
      真的
      -Wl,--添加stdcall别名
      -DBUILD__SO
      
      和本地测试2:

      <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>test</groupId>
             <artifactId>nativeParent</artifactId>
             <version>0.0.1-SNAPSHOT</version>
          </parent>
      
         <groupId>test</groupId>
         <artifactId>native2</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <packaging>dll</packaging>
      
         <build>
          <finalName>native1.so</finalName>
           <plugins>
             <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>native-maven-plugin</artifactId>
               <extensions>true</extensions>
               <configuration>
                 <compilerStartOptions>
                   <compilerStartOption>-Wl,--add-stdcall-alias</compilerStartOption>
                   <compilerStartOption>-DBUILD_DLL</compilerStartOption>
                 </compilerStartOptions>
      
               </configuration>
               </plugin>
      
               </plugins>
          </build>
      
      </project>
      
      
      4.0.0
      测试
      本地父母
      0.0.1-快照
      测试
      本地2
      0.0.1-快照
      动态链接库
      土生土长的
      org.codehaus.mojo
      原生maven插件
      真的
      -Wl,--添加stdcall别名
      -DBUILD\u动态链接库
      
      唯一不同的是包类型、目标名称和编译选项,但大多数配置都来自父pom

      请注意,只有一个compilerStartOption属性,因此尽管可以在子pom中使用多个同名属性,但会丢失父pom中所有同名的条目


      我希望这是您一直在寻找的东西-它仍然是一个样板代码,但它大大减少了。

      谢谢,我应该知道这样做(这确实有点帮助)。最大数量的样板来自三个
      执行
      目标的重复。。。这种父pom方法也可以用于此目的吗?