如何在gradle中以编程方式从src/java中排除顶级包?

b1uwtaje  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(354)

我有一个项目,有大约100个顶级包。我希望能够为这些顶级软件包制作一个jar。下面的代码可以找到顶级包,但是如何实际执行排除操作以便只得到一个?
最好将其概括为这样,这样就可以处理不在顶级的包。

sourceSets {
    List<String> folders=new LinkedList<>()
    String target="r"
    for(String path:main.java.srcDirs) 
        for(String name:new File(path).list())
            if(!name.equals(target))
                if(new File(path,name).isDirectory()) 
                    folders.add(name)
    println("how to exclude these folders: "+folders)

}
6g8kf2rb

6g8kf2rb1#

在空目录中,可以使用以下脚本创建源设置的模拟:

def dir = new File('src/main/java')

['a', 'b', 'c', 'd', 'e'].each { l1 -> 
  ['x', 'y', 'z', 'r', 'v'].each { l2 -> 
    def f = new File(dir, "$l1/$l2/SampleClass.java")
    f.parentFile.mkdirs()
    f.text = """\
      package ${l1}.${l2};

      class SampleClass { 

      }
    """.stripIndent()
  }
}

运行时,生成以下内容:

─➤ groovy write.groovy 

─➤ find src/main/java -type f | sort
src/main/java/a/r/SampleClass.java
src/main/java/a/v/SampleClass.java
src/main/java/a/x/SampleClass.java
src/main/java/a/y/SampleClass.java
src/main/java/a/z/SampleClass.java
src/main/java/b/r/SampleClass.java
src/main/java/b/v/SampleClass.java
src/main/java/b/x/SampleClass.java
src/main/java/b/y/SampleClass.java
src/main/java/b/z/SampleClass.java
src/main/java/c/r/SampleClass.java
src/main/java/c/v/SampleClass.java
src/main/java/c/x/SampleClass.java
src/main/java/c/y/SampleClass.java
src/main/java/c/z/SampleClass.java
src/main/java/d/r/SampleClass.java
src/main/java/d/v/SampleClass.java
src/main/java/d/x/SampleClass.java
src/main/java/d/y/SampleClass.java
src/main/java/d/z/SampleClass.java
src/main/java/e/r/SampleClass.java
src/main/java/e/v/SampleClass.java
src/main/java/e/x/SampleClass.java
src/main/java/e/y/SampleClass.java
src/main/java/e/z/SampleClass.java

─➤

以下是 build.gradle 在此目录树的根目录中:

apply plugin: 'java'

task jara(type: Jar) {
  archiveBaseName = 'jara'
  from jar.source
  include 'a/**/*'
}

task jarb(type: Jar) {
  archiveBaseName = 'jarb'
  from jar.source
  include 'b/**/*'
}

artifacts {
  archives jara, jarb
}

然后可以运行:

─➤ gradle clean build 

BUILD SUCCESSFUL in 509ms

产生:

─➤ ls -1 build/libs
groovy-how-to-programmatically-exclude-top-level-packages-from-src-java-in-gradle.jar
jara.jar
jarb.jar

─➤ unzip -l build/libs/jara.jar    
Archive:  build/libs/jara.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2020-12-21 15:56   META-INF/
       25  2020-12-21 15:56   META-INF/MANIFEST.MF
        0  2020-12-21 15:56   a/
        0  2020-12-21 15:56   a/r/
      266  2020-12-21 15:56   a/r/SampleClass.class
        0  2020-12-21 15:56   a/y/
      266  2020-12-21 15:56   a/y/SampleClass.class
        0  2020-12-21 15:56   a/z/
      266  2020-12-21 15:56   a/z/SampleClass.class
        0  2020-12-21 15:56   a/x/
      266  2020-12-21 15:56   a/x/SampleClass.class
        0  2020-12-21 15:56   a/v/
      266  2020-12-21 15:56   a/v/SampleClass.class
---------                     -------
     1355                     13 files

─➤ unzip -l build/libs/jarb.jar
Archive:  build/libs/jarb.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2020-12-21 15:56   META-INF/
       25  2020-12-21 15:56   META-INF/MANIFEST.MF
        0  2020-12-21 15:56   b/
        0  2020-12-21 15:56   b/r/
      266  2020-12-21 15:56   b/r/SampleClass.class
        0  2020-12-21 15:56   b/y/
      266  2020-12-21 15:56   b/y/SampleClass.class
        0  2020-12-21 15:56   b/z/
      266  2020-12-21 15:56   b/z/SampleClass.class
        0  2020-12-21 15:56   b/x/
      266  2020-12-21 15:56   b/x/SampleClass.class
        0  2020-12-21 15:56   b/v/
      266  2020-12-21 15:56   b/v/SampleClass.class
---------                     -------
     1355                     13 files

─➤

i、 e.两个jar文件,其中一个包含 a 顶级包,其中包含 b 顶级套餐。
不确定这是否符合你的目标,但它应该给你一些如何实现你想要的想法。

相关问题