0

After some hours of search and research I came up with a working example for writing the correct Maven dependencies into the generated pom.xml when using Gradle 1.11 and maven-publish plugin.

The first problem I faced was the provided dependency that always was written as runtime into the pom.xml

The second problem was the dynamic version, that I use for minor version changes. Maven and Gradle have different notations and the maven-publish simply writes the Gradle kind of notation into the pom.xml

Here is my example:

apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'de.pentos'
version = '0.4.4'
sourceCompatibility = 1.7
repositories {
    mavenCentral()
    mavenLocal()
}
configurations {
    provided
    compile.extendsFrom provided
}
dependencies {
    provided("org.projectlombok:lombok:1.+")
    provided("javax.servlet:javax.servlet-api:3.1.0")

    compile("org.slf4j:slf4j-api:1.7+")
    compile("com.fasterxml.jackson.core:jackson-databind:2.3.+")
    compile("joda-time:joda-time:2.3+")
    compile("org.springframework:spring-webmvc:3.2+")
    compile("org.springframework.security:spring-security-web:3.1+")

    testCompile("junit:junit:4.11")
}
jar { baseName = "${project.group}.${project.name}" }
publishing {
    publications {
        jar(MavenPublication) {
            from components.java
            artifactId "${project.name}"
            artifact sourceJar { classifier "sources" }
            pom.withXml {
                final Node root = asNode()
                final versionPattern = ~/(?:(.+)\.)?(.+?)\.?\+/
                configurations.compile.allDependencies.each {
                    final name = it.name
                    final group = it.group
                    final m = versionPattern.matcher(it.version)
                    if (m.matches()) {
                        final base = m[0][1]
                        final rest = m[0][2].toInteger()
                        final version = '[' + (base ? base + '.' : '') + (rest) + ',' + (base ? base + '.' : '') + (rest + 1) + ')'
                        root.dependencies.first().findAll{
                            it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
                        }.each {
                            it.version.first().value = version
                        }
                    }
                }
                configurations.provided.allDependencies.each {
                    final name = it.name
                    final group = it.group
                    root.dependencies.first().findAll{
                        it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
                    }.each {
                        it.scope.first().value = 'provided'
                    }
                }
            }
        }
    }
}
tasks.withType(Compile) { options.encoding = 'UTF-8' }
task sourceJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

So what is my Question?

I had some trouble with a lot examples that simply compared

..findAll {
    it.groupId == group
}

This hadn't worked for me and I had to find out by lot of testing how to get the findAll to work.
So is my version to detailed? Can it be written with less code?

Can I extract the functionality for dependency management into a more system wide script, that can be reused from all other projects, that need this dependency management? How?

During compileJava I get the warning :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
What does this mean, and how can I fix that.

Nabor
  • 1,661
  • 3
  • 20
  • 45

1 Answers1

0

I had some trouble with a lot examples that simply compared [...]

it.groupId is not a String. You'll need something like it.groupId.value(), or perhaps it.groupId[0].value().

Can I extract the functionality for dependency management into a more system wide script, that can be reused from all other projects, that need this dependency management? How?

You can write a plugin class, and ship it to builds as a Jar. For details, see the Gradle User Guide.

During compileJava I get the warning :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7 What does this mean, and how can I fix that.

This question has been asked and answered many times already. See Stack Overflow or http://forums.gradle.org.

PS: Instead of rewriting Ivy to Maven version range syntax, it should be possible to use Maven syntax from the start (i.e. in the build script).

Community
  • 1
  • 1
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • PS: Instead of rewriting Ivy to Maven version range syntax, it should be possible to use Maven syntax from the start (i.e. in the build script). You are indeed right, Gradle supports the other syntax out of the box and I haven't seen this in the documentation so fare – Nabor Apr 07 '14 at 17:20
  • Gradle supports the same version range syntax as Ivy, and Ivy supports some or all of the Maven syntax. – Peter Niederwieser Apr 07 '14 at 17:28
  • Besides that I understand you right that my solution for access the nodes with `it.groupId.first().value()[0]` is okay. It works, and there is no other way. I tested your suggestion. Instead of using `first()` this is also possible `it.groupId[0].value()[0]` – Nabor Apr 07 '14 at 17:29
  • I'd think that `it.groupId[0].value()` is enough, but I haven't verified this. – Peter Niederwieser Apr 07 '14 at 17:31
  • I did and `it.groupId[0].value() == group` is false where `it.groupId[0].value()[0] == group` returns true – Nabor Apr 07 '14 at 17:32
  • 1
    Instead you can do `it.groupId[0].text()`. – Peter Niederwieser Apr 07 '14 at 17:59
  • Regarding the source warning. If I understand it (http://goo.gl/mSB2Vt) correctly with Gradle you cannot compile against an older Java Version without having it installed on the system? In addition (http://goo.gl/VWeZJx or http://goo.gl/VZjS19) the path to the old JVM needs to be hard coded into the build.gradle? I'm spoilt by Eclipse. Here you only need one JVM and the runtime environments taking care that the generated class files are correct. Is this not possible using Gradle? – Nabor Apr 07 '14 at 18:05
  • 1
    It's just a *warning* issued by Oracle's Java compiler 1.7+ whenever you compile for an old Java version but against a newer Java API, which means you could accidentally use classes or methods that won't be available at runtime, without this being detected at compile time. (You can easily check for this on CI though.) The generated class files will be correct nevertheless. – Peter Niederwieser Apr 07 '14 at 18:17
  • 1
    If you do want to compile using an older JDK, you can either run Gradle with that JDK, or configure Gradle compile tasks with the old JDK path (which users could set in `init.gradle` or via an environment variable), or configure Gradle compile tasks with a bootstrap class path pointing to the old JDK. The Eclipse compiler simply doesn't issue such a warning. – Peter Niederwieser Apr 07 '14 at 18:18
  • PS: These should be separate questions which other Stack Overflow users can find and learn from. – Peter Niederwieser Apr 07 '14 at 18:20
  • Thank you for clarification. May be you add them to your answer and Users will find them better as in the comments. – Nabor Apr 07 '14 at 18:45
  • If this was useful, please accept my answer. And please ask individual questions next time. – Peter Niederwieser Apr 07 '14 at 18:50
  • The answer itself was a little bit vague, but the discussion was useful. – Nabor Apr 07 '14 at 19:12