0
0

More than 1 year has passed since last update.

gradleがpublishするpom.xmlにdependencyを入れる

Posted at

gradleでライブラリとしてjarをpublishしたときpom.xmlにdependenciesが含まれてなくてあれ? となった失敗談。その場合は、というか、基本的にはpublicationsartifact 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 jargradlew publishToMavenLocalをする。するとlocalのmaven-repositoryに以下のpom.xmlがpublishされる。

pom.xml
<?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
    }
  }
}

こうすると以下のように入ってくれる。

pom.xml
<?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てことらしい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0