publish grails3 plugin to artifactory - differences in generated poms - maven

I'm trying to publish a snapshot of grails 3 plugin to remote maven repository (Artifactory). Here is my build script:
import org.springframework.util.StringUtils
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
maven { url "${artifactory_contextUrl}"
credentials {
username "${artifactory_user}"
password "${artifactory_password}"
}
name = "remote-maven-repo"
}
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.2"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
id "com.jfrog.bintray" version "1.2"
}
String baseVersion = "0.1.0-%customer%-SNAPSHOT"
String currentVersion = "$baseVersion".replaceAll('%customer%',property('plugin.default.customer').toUpperCase())
version currentVersion
group "org.grails.plugins"
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "spring-boot"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"
println "Current customer mode: [${property("plugin.default.customer")}]"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
String defaultVersion = "default"
// Custom versioning scheme, take version of dependency with 'default' version from gradle.preperties
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.version == defaultVersion) {
customer = property("plugin.default.customer")
String version = resolveDefaultVersion(details.requested.group, details.requested.name, customer)
details.useVersion(version)
}
}
}
configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
String resolveDefaultVersion(String group, String name, String customer) {
if(group == 'com.mycompany.domain' && name == 'custom-lib'){
String version = property("custom-lib.${customer}.version")
println "resolved artifact [$group.$name] version [$version]"
return version
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
add buildscript.repositories.getByName("remote-maven-repo")
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
provided 'org.springframework.boot:spring-boot-starter-logging'
provided "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-autoconfigure"
provided "org.springframework.boot:spring-boot-starter-tomcat"
provided "org.grails:grails-web-boot"
provided "org.grails:grails-dependencies"
provided 'javax.servlet:javax.servlet-api:3.1.0'
testCompile "org.grails:grails-plugin-testing"
console "org.grails:grails-console"
//Security
compile "org.grails.plugins:spring-security-core:3.0.0.M1"
compile "org.grails.plugins:spring-security-acl:3.0.0.M1"
//Not grails3 compatible
//compile "org.grails.plugins:spring-security-oauth2-provider:2.0-RC5"
compile "org.grails.plugins:hibernate:4.3.10.6"
//configurable customer-dependent dependency, take version from gradle.properties
compile group: "com.mycompany.domain", name:"custom-lib", version:"$defaultVersion", changing: true
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
// gradle can't handle custom versioning scheme and will left com.mycompany.domain.custom-lib version value as 'default' in generated pom, we will fix it here.
pom.withXml {
asNode().get('dependencies').get(0).get('dependency').each { dep ->
String pomVer = dep.get("version").text()
String pomArtifactId = dep.get("artifactId").text()
String pomGroupId = dep.get("groupId").text()
if(pomVer == defaultVersion){
String customer = property("plugin.default.customer")
dep.get("version").get(0).setValue(resolveDefaultVersion(pomGroupId, pomArtifactId, customer))
}else if (StringUtils.isEmpty(pomVer)){
//gradle also left dependencies with provided scope in pom without version --> remove these dependencies
dep.replaceNode {}
}
}
}
}
}
repositories {
maven {
name 'myArtifactory'
url "${artifactoryContextUrl}/${(project.version.endsWith('-SNAPSHOT') ? snapshotRepoKey : releaseRepoKey)}"
credentials {
username = artifactory_user
password = artifactory_password
}
}
}
}
install.repositories.mavenInstaller.pom.whenConfigured { pom ->
pom.dependencies.findAll { it.version == defaultVersion }.each { dep ->
dep.version = resolveDefaultVersion(dep.groupId, dep.artifactId, property("plugin.default.customer"))
}
}
When I'm running gradle install - to install plugin in local m2 repo everything works fine, and pom looks good, it has dependencies with exactly the same scopes and versions, also provided dependencies are excluded from pom during publishing to local repository. When I'm running gradle publishMavenJavaPublicationToMyArtifactoryRepository pom is looking quite strange, gradle is ignoring defined in build script dependencies scopes and always using runtime. As an example:
pom after gradle install:
<dependency>
<groupId>org.grails.plugins</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.0.0.M1</version>
<scope>compile</scope>
</dependency>
pom after gradle publishMavenJavaPublicationToMyArtifactoryRepository:
<dependency>
<groupId>org.grails.plugins</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.0.0.M1</version>
<scope>runtime</scope>
</dependency>
Also pom after gradle install doesn't contain dependencies with scope provided. Pom.xml after gradle publishMavenJavaPublicationToMyArtifactoryRepository contains
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
without version, gradle doesn't like that.
Why am I receiving such different results? How can I generate the same pom for gradle publishMavenJavaPublicationToMyArtifactoryRepository as for gradle install. I don't like an opportunity to edit pom in publications section. Any help is highly appreciated.

This is a known limitation of the incubating new maven-publish plugin. The install task uses the "old"/stable Install task. If you really rely on these scopes, one workaround might be to use the gradle based "uploadArchives" task instead of relying on the bintray plugin for publishing your archives. Alternatively you can implement a fix by using the xml hooks of the new publish plugin.

Related

How to avoid repeating dependency versions in a multi-module Gradle project?

There's a sample Spring Boot project here that contains two modules.
The build.gradle for one of the modules looks like this:
buildscript {
ext { springBootVersion = '2.1.4.RELEASE' }
repositories { mavenCentral() }
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-multi-module-application'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile project(':library')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
The other module's build.gradle looks like this:
buildscript {
repositories { mavenCentral() }
}
plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" }
ext { springBootVersion = '2.1.4.RELEASE' }
apply plugin: 'java'
apply plugin: 'eclipse'
jar {
baseName = 'gs-multi-module-library'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
}
The springBootVersion = '2.1.4.RELEASE' is declared in both modules. For a 2 module project that might not be a problem, but if my project had 10 modules and I wanted to make sure that all modules always depend on the same version of Spring Boot, it would be inconvenient and error-prone to repeat this version in every module.
Similarly, I might want to add a dependency on commons-io to both of these modules, and ensure they both always depend on the same version of commons-io.
How can I avoid repeating the version numbers in each and every build.gradle file?
See this Gradle documentation : a good practice in Gradle is to configure subprojects which share common traits in a single place, for example in the root project's build script (or using custom plugins)
EDIT the solution proposed here is no longer considered as good practice from Gradle team (the link above does not even mention subproject bloc in latests Gradle version doc); thank you #buggy for the warning .
In your example taken from Spring boot documentation, this pattern could be applied to centralize Spring boot and other common dependencies versions in a single place, but you could go further and also configure other common traits (Java plugin configuration, repositoties, etc..)
Here is how I would re-write the Spring example to make it cleaner and DRY:
Root project
/**
* Add Springboot plugin into build script classpath (without applying it)
* This is this only place where you need to define the Springboot version.
*
* See https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation
*/
plugins {
id "org.springframework.boot" version "2.1.4.RELEASE" apply false
}
// Set version for dependencies share between subprojects
ext {
commonsIoVersion = "2.6"
}
subprojects {
// common config for all Java subprojects
apply plugin: "java"
apply plugin: "eclipse"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
// apply Spring Boot's dependency management plugin
apply plugin: "io.spring.dependency-management"
}
Library sub-project
// no need for additional plugins
jar {
baseName = 'gs-multi-module-library'
version = '0.0.1-SNAPSHOT'
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
implementation "commons-io:commons-io:${commonsIoVersion}"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
Application sub-project
plugins {
id "org.springframework.boot"
}
bootJar {
baseName = 'gs-multi-module-application'
version = '0.0.1-SNAPSHOT'
}
dependencies {
implementation project(':library')
implementation ('org.springframework.boot:spring-boot-starter-actuator')
implementation ('org.springframework.boot:spring-boot-starter-web')
implementation "commons-io:commons-io:${commonsIoVersion}"
// could also be configured in root project.
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Notes
this solution uses the new plugins {} DSL only (no need for old buildscript block)
version of the io.spring.dependency-management should not be configured explicitly, it will be inherit from Spring boot plugin
You can move the ext{} block to a new file and reference it in your project's build.gradle file via the apply from: statement.
// project/versions.gradle
ext {
springBootVersion = '2.1.4.RELEASE'
}
// project/build.gradle
buildscript {
apply from: 'versions.gradle'
}
// module/build.gradle
dependencies {
implementation "some.dependency:dependency:$springBootVersion"
}
Now you only have to define your dependency versions in one place.
Typically, a project will have a project-level build.gradle file in addition to module-specific build.gradle files. However, the repo you shared is missing the project-level build script. This is why the ext{} block is defined in each module's build script. This is likely not optimal, and I recommend looking at other repos to see how different developers tackled this issue.
There's a Gradle consistent versions plugin that will handle this:
https://github.com/palantir/gradle-consistent-versions
After you set this plugin up in the root build.gradle file, you will create a versions.props file like this, to use the example from the documentation.
com.fasterxml.jackson.*:jackson-* = 2.9.6
com.google.guava:guava = 21.0
com.squareup.okhttp3:okhttp = 3.12.0
junit:junit = 4.12
org.assertj:* = 3.10.0
There's also a versions.locks file, which is auto-generated by ./gradlew --write-locks. You can then specify version-less dependencies in your build.gradle files.

Gradle spring boot 2 spring-boot-dependencies cannot download dependencies

I am using eclipse 2018, gradle 5.2.1, buildship 3.0.1.
My config looks like:
I try to create spring boot 2 according to building-spring-boot-2-projects-with-gradle
The build.gradle is:
plugins {
id 'java'
id 'com.gradle.build-scan' version '2.1'
id 'org.springframework.boot' version '2.1.3.RELEASE'
}
repositories {
jCenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.1.3.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
// Need to patch constraints because snakeyaml is an optional dependency
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.23' } }
}
}
}
}
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
bootJar {
mainClassName = 'gt4.App'
}
However, after I save build.gradle, the Project and External Dependencies disappear, and the spring boot jars are not downloaded too.
What I was wrong?
If I create spring boot project with gradle by Spring Tool Suite 4, the generated build.gradle is:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
then it works.
Do I use spring-boot-dependencies wrong?
try to deleting the local Repository and build project again to download dependancy.
Delete .m2 or .gradle folder and then Rebuild your project.
a)On a Windows machine, the .gradle or .m2 path will be:
c:\Users\username\.m2 or c:\Users\username\.m2
b)On linux machine, the .gradle or .m2 path will be:
USER_HOME/.m2/

Create external dependencies and add it to other project with gradle

I have two spring boot projet
A and B
would like to create a new projet to put common thing there
A
B
commons
how to add commons to A and B like a external dependencies?
Assume your project layout as below:
project
|- common
|- proja
|- projb
you need settings.gradle
rootProject.name = 'project'
include 'common'
include 'proja'
include 'projb'
then you need to update your build.gradle under project like this:
plugins {
id 'org.springframework.boot' version '1.5.4.RELEASE'
}
subprojects { // common configurations for all subprojects
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
repositories {
jcenter()
}
dependencies { // common dependencies for all subprojects
compile 'org.codehaus.groovy:groovy-all:2.4.10'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'
}
}
project ("proja") {
dependencies { // proja-specific dependencies
compile 'com.google.guava:guava:21.0'
compile project(":common")
}
}
project ("projb") {
dependencies { // projb-specific dependencies
compile project(":common")
}
}
Let me know if this works.
external solution seem the way to go
my commons build.gradle file
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.0.M2'
}
distributions {
main{
baseName = 'commons-code'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}
In my main project
i have an build.gradle
in the dependencies section i have
compile project (':commons-code')
and in the settings.gradle file i have
include ":commons-code"
project(":commons-code").projectDir = file("/home/bob/dev/project/commons/")

Spring Batch with Grails

im currently using grails 3.0.3 along with gradle 2.5 and hoping to generate a plugin that will use Spring Batch. I've just created a plugin via the command line, and update the build.gradle with just the
compile "org.springframework.boot:spring-boot-starter-batch"
However the batch folder does not seem to downloaded to the local .m2 cache and therefore the import org.springframework.batch libs not available.
The following is the build.gradle. Just wondering if anybody could suggest the reasons why?
Thanks
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
id "com.jfrog.bintray" version "1.2"
}
version "0.1-SNAPSHOT"
group "org.grails.plugins"
apply plugin: 'maven-publish'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "spring-boot"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"
// Used for publishing to central repository, remove if not needed
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/grailsCentralPublishing.gradle'
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/bintrayPublishing.gradle'
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
provided 'org.springframework.boot:spring-boot-starter-logging'
provided "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-autoconfigure"
provided "org.springframework.boot:spring-boot-starter-tomcat"
provided "org.grails:grails-web-boot"
provided "org.grails:grails-dependencies"
provided 'javax.servlet:javax.servlet-api:3.1.0'
testCompile "org.grails:grails-plugin-testing"
console "org.grails:grails-console"
//compile "org.springframework.boot:spring-boot-starter-batch:1.2.5.RELEASE"
compile "org.springframework.boot:spring-boot-starter-batch"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}

build.gradle - error deploying artifact

I'm having issues with publishing to local nexus maven repository.
I admit, I don't have much experience with using gradle.
I will say that I have tried understanding the documentation and examples that are given through the gradle website (along with a few stackoverflow questions).
I am getting the following error when I try to publish:
Execution failed for task ':publishMavenPublicationToMavenRepository'.
> Failed to publish publication 'maven' to repository 'maven'
> Error deploying artifact 'com.myproject:myproject-sdk:jar': Error deploying artifact: Resource to deploy not found: File: http://git.site.com:8081/nexus/content/repositories/releases/com/myproject/myproject-sdk/3.0.0/myproject-sdk-3.0.0.jar does not exist
the entire build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'build-version'
buildscript {
repositories {
maven { url "http://git.site.com:8081/nexus/content/groups/public" }
maven { url 'https://geonet.artifactoryonline.com/geonet/public-releases' }
mavenCentral()
}
dependencies {
classpath 'nz.org.geonet:gradle-build-version-plugin:1.+'
}
}
repositories {
maven {
url "http://git.site.com:8081/nexus/content/groups/public"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.code.gson:gson:2.2.4'
compile 'commons-codec:commons-codec:1.9'
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
maven(MavenPublication) {
artifactId = 'myproject-sdk'
groupId = 'com.myproject'
version '3.0.0'
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
repositories {
maven {
// nexus maven credentials
credentials {
username "MY_USERNAME"
password ""MY_PASSWORD"
}
if(version.endsWith('-SNAPSHOT')) {
url "http://git.site.com:8081/nexus/content/repositories/snapshots"
} else {
url "http://git.site.com:8081/nexus/content/repositories/releases"
}
}
}
}
I am using Android Studio and this project is a Java project.
I don't understand the message because it says "Resource to deploy not found" and it points to a url at nexus.
I would expect a message like "Resource to deploy not found: File: c:/directory_that_doesn't_exist/whatever.jar"
Additional Information -
The gradle tasks listed under 'All tasks' are:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
generatePomFileForMavenPublication
jar
javadoc
processResources
processTestResources
publish
publishMavenPublicationToMavenLocal
publishMavenPublicationToMavenRepository
publishToMavenLocal
sourceJar
test
testClasses

Resources