问题描述
假设我正在使用Gradle进行模块化库开发.在我的根项目中,我有子项目 geometry
, algorithms
, visualizer
,并且我想发布每个项目的jar项目.
Suppose I'm using Gradle for a modular library development. In my root project I have subprojects geometry
, algorithms
, visualizer
, and I'd like to publish a jar artifact of each.
到目前为止,在我的根目录 build.gradle
中,我有以下部分:
As for now in my root build.gradle
I have the following part:
apply plugin: 'maven-publish'
publishing {
publications {
publishDemos(MavenPublication) {
groupId 'ru.ifmo.ctddev.igushkin.cg'
artifactId 'geometry'
version globalVersion
artifact project(':geometry').tasks.getByName('jar')
}
publishAlgorithms(MavenPublication) {
groupId 'ru.ifmo.ctddev.igushkin.cg'
artifactId 'algorithms'
version globalVersion
artifact project(':algorithms').tasks.getByName('jar')
}
publishVisualizer(MavenPublication) {
groupId 'ru.ifmo.ctddev.igushkin.cg'
artifactId 'visualizer'
version globalVersion
artifact project(':visualizer').tasks.getByName('jar')
}
}
}
我的第一个问题:描述出版物的方式是否更短?例如,我要声明,对于每个子项目,我需要一个出版物,该出版物的名称应设置为 artifactId
.
My first question: is there a shorter way of describing the publications? For example, I'd like to state that for each subproject I need a publication with the artifactId
set from its name.
接下来,我的子项目相互依赖,算法
和 visualizer
都依赖于 geometry
中的类,但是在这一点上,罐子确实不包含依赖项,例如,如果用户想使用 algorithms
,则必须将其添加到 geometry
和 algorithms
中.
Next, my subprojects depend on each other, both algorithms
and visualizer
depend on classes from geometry
, but at this point the jars do not contain the dependencies, and, for example, the user will have to add dependencies to both geometry
and algorithms
if they want to use algorithms
.
那么,有没有一种方法可以提供某种自动相关性,以便添加算法
也会添加 geometry
?如果是,该怎么办?如果没有,提供模块化库的惯用方式是什么?我应该用依赖项组装罐子吗?
So, is there a way for to provide some sort of auto-dependency, so that adding algorithms
would also add geometry
? If yes, how do I do it? If no, what is the idiomatic way of providing modular libraries? Should I assemble jars with dependencies instead?
UPD:是的,我应该使用project(':...').components.java 中的,而不是
artifact ...
将同时获取工件和依赖项?如果我使用 artifact ...
,如何分别选择依赖项?
UPD: Am I right that instead of artifact ...
I should just use from project(':...').components.java
, because it will pick up both artifacts and dependencies? How do I pick dependencies separately if I use artifact ...
?
推荐答案
通过将相同的发布配置注入到每个子项目中,可以减少冗长的发布声明.例如,对于具有以下结构的多层建筑项目:
You can do less verbose publication declaration by injecting the same publication config into each subproject. For example for a multi-build project with the structure:
ROOT
│ build.gradle
│ settings.gradle
├───subA
│ build.gradle
│
├───subB
│ build.gradle
│
└───subC
build.gradle
在您的根 build.gradle
中,您可以执行以下操作:
In your root build.gradle
, you can do:
apply plugin:'maven-publish'
subprojects{
publishing {
publications {
"$project.name"(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
from components.java
}
}
}
}
每个子项目都定义自己的groupid和版本,如下所示:
Each subproject defines its own groupid and version like so:
group = 'org.test.sample.A'
version = '1.0'
artifactId是从子项目名称中选取的.运行 gradle publish
会得到以下结构的存储库:
The artifactId is picked up from the subproject name. Running gradle publish
results in a repo of this structure:
org
└───test
└───sample
├───A
│ └───subA
│ └───1.0
│ subA-1.0.jar
│ subA-1.0.pom
├───B
│ └───subB
│ └───1.0
│ subB-1.0.jar
│ subB-1.0.pom
└───C
└───subC
└───1.0
subC-1.0.jar
subC-1.0.pom
依赖项
此配置还自动处理依赖项.例如,如果在子项目 subA
中,您拥有:
dependencies{
compile project(':subB')
}
由于我使用的是来自components.java 的而不是
artifact
的插件,因此该插件知道要查找依赖项并为 subA
生成pom包括:
Since I am using from components.java
instead of artifact
, the plugin knows to look for dependencies and generates a pom for subA
that includes:
<dependencies>
<dependency>
<groupId>org.gradle.sample.B</groupId>
<artifactId>subB</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
这篇关于使用Gradle将模块化库发布到Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!