简体   繁体   English

Gradle 可以将多个项目打包到一个 jar 中吗?

[英]Can Gradle jar multiple projects into one jar?

Can Gradle jar multiple projects into one jar? Gradle 可以将多个项目打包到一个 jar 中吗?

I know you can do it for a single project using a method like this:我知道您可以使用如下方法为单个项目执行此操作:

task packageTests(type: Jar) {
  from sourceSets.test.classes
}

But how does a person zip up multiple sub-projects into one jar?但是一个人如何将多个子项目压缩到一个罐子里呢?

I tried this and it doesn't work:我试过这个但它不起作用:

task packageTests(type: Jar) {
  from project(':core').sourceSets.main.classes
  from project(':core:google').sourceSets.test.classes
  from project(':core:bing').sourceSets.test.classes
}

This should work for what you want to do.这应该适用于你想做的事情。 This should be in the root gradle build file.这应该在根 gradle 构建文件中。

subprojects.each { subproject -> evaluationDependsOn(subproject.path)}

task allJar(type: Jar, dependsOn: subprojects.assemble) {
   baseName = 'your-base-name'
   subprojects.each { subproject -> 
      from subproject.configurations.archives.allArtifacts.files.collect {
         zipTree(it)
       }
    }
 }

You can publish this by adding it as an archive:您可以通过将其添加为存档来发布它:

artifacts {
   archives allJar
}

Here's my solution, which is a little bit simpler:这是我的解决方案,它有点简单:

// Create a list of subprojects that you wish to include in the jar.  
def mainProjects = [':apps',':core',':gui',':io']
task oneJar( type: Jar , dependsOn: mainProjects.collect{ it+":compileJava"}) {
    baseName = 'name of jar'
    from files(mainProjects.collect{ project(it).sourceSets.main.output })
}

Code has been tested on Gradle 1.12代码已在 Gradle 1.12 上测试

The following solution is quite similar to the proposed by CaTalyst.X but uses jar task directly.以下解决方案与CaTalyst.X提出的解决方案非常相似,但直接使用jar任务。

subprojects.each { subproject -> evaluationDependsOn( subproject.path ) }
jar.dependsOn subprojects.tasks['classes']
jar {
  baseName = 'MyApp'
  manifest {
    attributes 'Main-Class': 'org.abc.App'
  }
  subprojects.each { subproject ->
    from subproject.sourceSets.main.output.classesDir
    from subproject.sourceSets.main.output.resourcesDir
  }
}

It was tested against Gradle 2.1 and 2.2.1它针对 Gradle 2.1 和 2.2.1 进行了测试

I know that this has been answered, but since the answer is old (current Gradle version is few major versions newer - 6.2.2) and this thread shows up in search engines quite high I'll post solution that worked for me.我知道这已经得到回答,但由于答案是旧的(当前的 Gradle 版本是几个较新的主要版本 - 6.2.2)并且该线程在搜索引擎中显示得非常高,我将发布对我有用的解决方案。

Generally my problem was similar to/same as the one stated in original post: I wanted to get classes from all subprojects and zip them into one jar (preferably using the most default settings as possible).一般来说,我的问题与原始帖子中所述的问题相似/相同:我想从所有子项目中获取类并将它们压缩到一个 jar 中(最好尽可能使用最默认的设置)。 Finally I've found that the following code does the trick:最后我发现下面的代码可以解决问题:

jar {
  from subprojects.sourceSets.main.output
}

But while it builds jar properly, I also wanted to be able to publish this jar to Maven repository as single artifact which depends on everything that all subprojects do.但是当它正确构建 jar 时,我还希望能够将这个 jar 作为单个工件发布到 Maven 存储库,这取决于所有子项目所做的一切。 To achieve this the main project has to dependent on subprojects dependencies which is done by:为此,主项目必须依赖于子项目的依赖关系,这是通过以下方式完成的:

subprojects.each { evaluationDependsOn(it.path) }
dependencies {
  api(externalSubprojectsDependencies('api'))
  implementation(externalSubprojectsDependencies('implementation'))
}
private externalSubprojectsDependencies(String configuration) {
  subprojects.collect { it.configurations[configuration].allDependencies }.flatten()
      .findAll { !it.properties['dependencyProject'] } // this looks like hack, I'd love to see better solution
}

Only external dependencies are copied - we do not want internal (project) dependencies listed in pom.xml as all their classes are in jar anyway.复制外部依赖项——我们不希望在pom.xml中列出内部(项目)依赖项,因为它们的所有类无论如何都在 jar 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM