Publish Artifacts from a Gradle Project
Suppose you have a project that uses Gradle build tool and want to publish project artifacts to the newly created Maven repository using:
Note that publishing artifacts with the same package version is not allowed. The server will return the 409 HTTP response. The only exception: The artifacts with the SNAPSHOT
prefix can be published to a repository with the enabled snaphsots support.
Publish Maven artifacts using Gradle command-line tool
To publish artifacts of a Gradle project, you should configure artifact properties, reference a repository, and specify authentication credentials. This configuration is performed in the build.gradle
file.
Open the project's
build.gradle
file.Add the reference to the Maven plugin:
apply plugin: 'maven-publish'In the
publishing
section, specify the package properties (the generated package name will begroupId:artifactId
). For example, we want to publish a .jar file (for the sake of length, the section content is reduced):publishing { publications { maven(MavenPublication) { groupId = 'org.company' artifactId = 'sample' version = "0.9-SNAPSHOT" from components.java pom { name = 'My Library' description = 'A description of my library' ... } } } }In the
repositories
sub-section, add the repository settings including the credentials and the URL. As it's not secure to explicitly specify credentials inbuild.gradle
, you should use variables, which in our example are$usr
and$pwd
. We'll assign values to these variables in the next step.publishing{ ... repositories { maven { credentials { username = "$usr" password = "$pwd" } url = "https://maven.pkg.jetbrains.space/mycompany/p/projectkey/my-maven-repo" } } }You can set variable values in the
gradle.properties
file, which is stored locally on your machine. The default location of this file is:on Windows:
%HOMEPATH%\.gradle\gradle.properties
on Linux / macOS:
~/.gradle/gradle.properties
Open
gradle.properties
and specify variable values. Here you must use the credentials of either your own Space account (using a permanent token instead of a password is strongly recommended) or a separate service account:usr = admin pwd = 1234That's it! Now, we can publish project artifacts by using, for example, the Gradle command-line tool or project's Gradle wrapper.
gradlew publish
Publish Maven artifacts from JetBrains TeamCity
Before configuring TeamCity, make sure your project is configured as described in Publish Maven artifacts using Gradle command-line tool. If it's done, then all you need is to authenticate TeamCity in Space and add a gradle publish
build step.
Log in to your JetBrains TeamCity instance.
In Administration, in project settings, open the desired build configuration.
In Parameters, add two system properties for the username and the password. Note that property names must match the corresponding variable names from
build.gradle
. For example:system.usr (if in
build.gradle
, you've specifiedusername = $usr
).system.pwd (if in
build.gradle
, you've specifiedpassword = $pwd
). In Spec, type password. This will hide the password from logs, reports, and so on.
Finally, you should get something like this:
Add a Gradle build step that runs
gradle publish
. No additional configuration is needed:
Publish Maven artifacts from Space Automation
For details, refer to the Space Automation examples.