想从Gradle fat jar中移除特定的jar文件

5

我的当前build.gradle文件如下所示。

repositories {
    flatDir {
       dirs 'lib'
   }
   maven {      
    mavenCentral()
  }

 dependencies {    

  compile "commons-logging:commons-logging:1.0.4", 
          "org.apache.httpcomponents:httpclient:4.4",
          "org.apache.httpcomponents:httpcore:4.4",
          "com.fasterxml.jackson.core:jackson-annotations:2.5.1",             
          "joda-time:joda-time:2.7",


  compile files('../lib/abc.jar')
 }

 jar {
    manifest{
        attributes ("Version" : project.version, "${parent.manifestSectionName}")
        attributes ("Name" : project.name, "${parent.manifestSectionName}")     
    }

    from {
          configurations.runtime.filter( {! (it.name =~ /abc.*\.jar/ )}).collect {
             it.isDirectory() ? it : zipTree(it)
         }
    }
  }  

从上面的例子可以看出,我已经在运行时删除了abc.jar,但同样地,我想删除更多的jar包。简而言之,我想在fat jar中包含一些jar包,并排除一些jar包。那么我该如何实现呢?

1个回答

0
以下示例可能会对您有所帮助。您需要添加新的配置 - 它默认情况下会 扩展 编译,因此在开发过程中可用,但不会包含在生成的 jar 文件中 - 就像下面的 joda 示例一样。
apply plugin: 'java'

repositories {
  mavenCentral()
}

configurations {
  lol
}

dependencies {    

  compile "commons-logging:commons-logging:1.0.4", 
          "org.apache.httpcomponents:httpclient:4.4",
          "org.apache.httpcomponents:httpcore:4.4",
          "com.fasterxml.jackson.core:jackson-annotations:2.5.1"

  lol "joda-time:joda-time:2.7"
}

jar {
  from {
    configurations.runtime.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
}  

尝试了这种方法,但是没有成功。它仍然在fat jar中包含joda-time。 - amuser
当我运行 gradle clean jar,然后提取已构建的jar文件时,发现没有joda包,这很奇怪。 - Opal
build 只是一个聚合任务。在这种情况下,joda 也缺失了。 - Opal
请尝试使用 configurations.runtime - configurations.lol - Opal
1
但是这并没有起作用。它无法解决commons-logging jar的依赖关系,因此在构建时会抛出错误。 - amuser
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接