gradleでライブラリとしてjarをpublishしたときpom.xmlにdependenciesが含まれてなくてあれ? となった失敗談。その場合は、というか、基本的にはpublications
でartifact jar
ではなくcomponents.java
を使用する。
環境
- gradle 7.3
ソースコードなど
まず適当なdependency
を含む適当なbuild.gradle
を作成する。
plugins {
id 'java-library'
id 'maven-publish'
}
group = 'kagamihoge.example'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.guava:guava:31.0.1-jre'
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact jar
}
}
}
上記の通りartifact jar
でgradlew publishToMavenLocal
をする。するとlocalのmaven-repositoryに以下のpom.xml
がpublishされる。
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>kagamihoge.example</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
今回はここにguava
が入っていて欲しいけど入ってない。そうしたい場合、以下のようにartifact jar
ではなくfrom components.java
を使用する。
publishing {
publications {
mavenJava(MavenPublication) {
//artifact jar
from components.java
}
}
}
こうすると以下のように入ってくれる。
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>kagamihoge.example</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
マニュアルによると、
components.java
A SoftwareComponent for publishing the production JAR created by the jar task. This component includes the runtime dependency information for the JAR.
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_publishing
This component includes the runtime dependencyてことらしい。