Gradle 在目录中包含传递依赖
Gradle include transitive dependencies in directory
我有一个 java 项目,它有几个依赖项,例如:
dependencies {
compile("com.whatever.jar:jar-1:1.2.3")
compile("com.whatever.jar:jar-2:4.5.6")
compile("com.whatever.jar:jar-3:7.8.9")
}
我将其打包为一个 jar 库,并将其与描述这些依赖项的 POM 文件一起发布到存储库:
<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>com.my.project</groupId>
<artifactId>my-library</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-1</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-2</artifactId>
<version>4.5.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-3</artifactId>
<version>7.8.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
我还有一个另一个项目,它依赖于这个以前在编译时构建的jar:
dependencies {
compileOnly("com.my.project:my-library:1.0.0")
}
我想创建一个构建第二个项目的任务,并创建一个包含 com.my.project:my-library:1.0.0
jar 及其传递依赖项(jar-1
、jar-2
和 jar-3
).
task createZip << {
into('lib') {
// here I would like to do something like (it's a horrible pseudo-code):
from configurations.(com.my.project:my-library:1.0.0).jar.transitiveDependencies.files
}
}
我该如何实现?
编辑:我还想以编程方式访问 my-library
的传递依赖项列表(准确地说是那些,而不是项目中的任何其他依赖项)以构建 Loader-Path
清单使用那些属性。
task uberJar(type: Jar) {
dependsOn classes
from sourceSets.main.runtimeClasspath.collect { it.directory?fileTree(it):zipTree(it)}
classifier = 'uber-jar'
}
assemble.dependsOn uberJar
这是一个自包含的 build.gradle
脚本,它演示了如何创建您的 createZip
任务(仅向 ZIP 存档添加特定依赖项):
/* make the "compileOnly" configuration available */
plugins {
id 'java'
}
/* add some demo dependencies to the project */
repositories {
jcenter()
}
dependencies {
compileOnly 'junit:junit:4.12'
compileOnly 'org.slf4j:slf4j-simple:1.7.28'
}
task createZip(type: Zip) {
// the "lib" directory in the ZIP file will only have all those dependency
// files of the "compileOnly" configuration which belong to the
// "junit:junit:4.12" module:
def depOfInterest = dependencies.create('junit:junit:4.12')
into('lib') {
from configurations.compileOnly.resolvedConfiguration.getFiles({dep ->
dep == depOfInterest
})
}
// the "justForDemonstration" directory in the ZIP file will have all
// dependency files of the "compileOnly" configuration, incl. SLF4J:
into('justForDemonstration') {
from configurations.compileOnly
}
}
这可以是 运行 和 ./gradlew createZip
(使用 Gradle 5.6.2 测试)。它在 build/distributions/<your_project_name>.zip
下生成 ZIP 文件。让我知道你的伪代码是否就是这么想的。
我有一个 java 项目,它有几个依赖项,例如:
dependencies {
compile("com.whatever.jar:jar-1:1.2.3")
compile("com.whatever.jar:jar-2:4.5.6")
compile("com.whatever.jar:jar-3:7.8.9")
}
我将其打包为一个 jar 库,并将其与描述这些依赖项的 POM 文件一起发布到存储库:
<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>com.my.project</groupId>
<artifactId>my-library</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-1</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-2</artifactId>
<version>4.5.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-3</artifactId>
<version>7.8.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
我还有一个另一个项目,它依赖于这个以前在编译时构建的jar:
dependencies {
compileOnly("com.my.project:my-library:1.0.0")
}
我想创建一个构建第二个项目的任务,并创建一个包含 com.my.project:my-library:1.0.0
jar 及其传递依赖项(jar-1
、jar-2
和 jar-3
).
task createZip << {
into('lib') {
// here I would like to do something like (it's a horrible pseudo-code):
from configurations.(com.my.project:my-library:1.0.0).jar.transitiveDependencies.files
}
}
我该如何实现?
编辑:我还想以编程方式访问 my-library
的传递依赖项列表(准确地说是那些,而不是项目中的任何其他依赖项)以构建 Loader-Path
清单使用那些属性。
task uberJar(type: Jar) {
dependsOn classes
from sourceSets.main.runtimeClasspath.collect { it.directory?fileTree(it):zipTree(it)}
classifier = 'uber-jar'
}
assemble.dependsOn uberJar
这是一个自包含的 build.gradle
脚本,它演示了如何创建您的 createZip
任务(仅向 ZIP 存档添加特定依赖项):
/* make the "compileOnly" configuration available */
plugins {
id 'java'
}
/* add some demo dependencies to the project */
repositories {
jcenter()
}
dependencies {
compileOnly 'junit:junit:4.12'
compileOnly 'org.slf4j:slf4j-simple:1.7.28'
}
task createZip(type: Zip) {
// the "lib" directory in the ZIP file will only have all those dependency
// files of the "compileOnly" configuration which belong to the
// "junit:junit:4.12" module:
def depOfInterest = dependencies.create('junit:junit:4.12')
into('lib') {
from configurations.compileOnly.resolvedConfiguration.getFiles({dep ->
dep == depOfInterest
})
}
// the "justForDemonstration" directory in the ZIP file will have all
// dependency files of the "compileOnly" configuration, incl. SLF4J:
into('justForDemonstration') {
from configurations.compileOnly
}
}
这可以是 运行 和 ./gradlew createZip
(使用 Gradle 5.6.2 测试)。它在 build/distributions/<your_project_name>.zip
下生成 ZIP 文件。让我知道你的伪代码是否就是这么想的。