Skip to content

dkorotych/gradle-maven-exec-plugin

Repository files navigation

Gradle plugin which provides a Maven exec task

Build Status codecov Codacy Badge Codacy Badge license

Installing

Releases of this plugin are hosted on Gradle's Plugin Repository. Apply the plugin to your project using one of the two methods below.

Using the plugins DSL:

plugins {
    id "com.github.dkorotych.gradle-maven-exec" version "3.0.1"
}
buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.github.dkorotych.gradle.maven.exec:gradle-maven-exec-plugin:3.0.1"
    }
}

apply plugin: "com.github.dkorotych.gradle-maven-exec"

Usage

This plugin enables a MavenExec task type in your buildscript which behaves exactly like a typical Gradle Exec task, except that it normalizes calls to work across operating systems. It does this by:

  1. Automatic generation of correct command line
  2. Used Maven Wrapper if it exists in a project
  3. It is forbidden to direct control of the command line arguments
  4. Removing unsupported command-line options when you call different Maven versions

To define a MavenExec task, simply specify the task type:

task rebuild(type: MavenExec) {
    goals 'clean', 'package'
}

or passing the advanced command-line options:

task generateSite(type: MavenExec) {
    goals 'clean', 'package', 'site'
    options {
        threads = '2C'
        activateProfiles = ['development', 'site']
        define = [
            'groupId'   : 'com.github.application',
            'artifactId': 'parent'
        ]
    }
}

or used it without options closure:

task generateSite(type: MavenExec) {
    goals 'archetype:generate'
    define([
        'groupId'            : 'com.mycompany.app',
        'artifactId'         : 'my-app',
        'archetypeArtifactId': 'maven-archetype-quickstart',
        'interactiveMode'    : 'false'
    ])
    quiet true
}

or even a direct call to control Maven from any task

task generateAndExecuteApplication {
    doLast {
        buildDir.mkdirs()
        def groupId = 'com.mycompany.app'
        def application = 'my-app'
        def applicationDir = file("$buildDir/$application")
        delete applicationDir
        mavenexec {
            workingDir buildDir
            goals 'archetype:generate'
            define([
                    'groupId'            : groupId,
                    'artifactId'         : application,
                    'archetypeArtifactId': 'maven-archetype-quickstart',
                    'interactiveMode'    : 'false'
            ])
            quiet true
        }
        mavenexec {
            workingDir applicationDir
            goals 'package'
            threads '2C'
            quiet true
        }
        javaexec {
            workingDir applicationDir
            classpath file("$applicationDir/target/$application-1.0-SNAPSHOT.jar")
            main "${groupId}.App"
        }
    }
}

F.A.Q

If you run into any issues, consult the F.A.Q. first.