Alistair
Alistair

Reputation: 1979

Gradle - load jar from maven and use as part of build

From my gradle build I want to minify my html as part of the build process using html compressor, which exists as a maven artifact: http://code.google.com/p/htmlcompressor/#Maven_Artifact

The jar should be loaded from maven central and new versions should be automatically used as they are released.

How can this be achieved?

Upvotes: 2

Views: 353

Answers (2)

VolkerK
VolkerK

Reputation: 1510

The jar should be loaded from maven central and new versions should be automatically used as they are released.

Can be done with the following dependency:

dependencies {
    htmlcompressor 'com.googlecode.htmlcompressor:htmlcompressor:latest.integration'
}

Upvotes: 0

Benjamin Muschko
Benjamin Muschko

Reputation: 33426

Currently, no Gradle plugin exists to simplify this task that I am aware of. I think it would be great if you would write one and contribute it to the community. For now you could probably use the Ant tasks provided by htmlcompressor. Make sure the input and output directories actually exist before running the task. The version qualifier in the dependency definition lets you pull newer versions by using a plus sign e.g. 1.+. I would not recommend doing that as it might break your build if the Ant task definition changes with a newer version.

configurations {
    htmlcompressor
}

repositories {
    mavenCentral()
}

dependencies {
    htmlcompressor 'com.googlecode.htmlcompressor:htmlcompressor:1.4'
}

task compressHtml << {
    ant.apply(executable: "java", parallel: false) {
        fileset(dir: "test", includes: "*.html") {
            exclude(name: "**/leave/**")
        }

        arg(value: "-jar")
        arg(path: configurations.htmlcompressor.asPath)
        arg(line: "--type html")
        arg(value: "--preserve-comments")
        srcfile()
        arg(value: "-o")
        mapper(type: "glob", from: "*", to: "compressed/*")
        targetfile()
    }
}

EDIT: You don't actually need to add the dependency to the script's classpath. Using a configuration for it is much cleaner. I changed the script to reflect that.

Upvotes: 3

Related Questions