java Gradle 可以将多个项目合并到一个 jar 中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13782013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:57:40  来源:igfitidea点击:

Can Gradle jar multiple projects into one jar?

javabuildgradle

提问by djangofan

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
}

采纳答案by lessthanoptimal

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 上测试

回答by CaTalyst.X

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
}

回答by Pawe? Grze?

The following solution is quite similar to the proposed by CaTalyst.Xbut uses jartask 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 进行了测试

回答by telebin

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). Finally I've found that the following code does the trick:

通常,我的问题与原始帖子中所述的问题相似/相同:我想从所有子项目中获取类并将它们压缩到一个 jar 中(最好尽可能使用大多数默认设置)。最后我发现以下代码可以解决问题:

jar {
  from subprojects.sourceSets.main.output
}

Butwhile 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. To achieve this the main project has to dependent on subprojects dependencies which is done by:

但是,虽然它正确构建了 jar,但我还希望能够将此 jar 作为单个工件发布到 Maven 存储库,这取决于所有子项目所做的一切。为了实现这一点,主项目必须依赖于子项目的依赖,这是通过以下方式完成的:

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.xmlas all their classes are in jar anyway.

复制外部依赖项- 我们不希望列出内部(项目)依赖项,pom.xml因为无论如何它们的所有类都在 jar 中。