Maven plugin configuration to gradle

I’m trying to convert this plugin in pom to gradle. And I’ve added compile 'org.codehaus.mojo:exec-maven-plugin:1.2.1' in the dependencies. There are some other parameters like “includeProjectDependencies” and so on. Just don’t know what else to put in “JavaExec”:

task executeJava(type:JavaExec) {
   main = "com.abc.efg.func.DocumentGenerator"
   classpath = sourceSets.main.runtimeClasspath
   dependsOn = files('/lib/tool.jar')
   args = [
        "--api_list",
        "metaapi@v1",
        "campaign@v1",
        "--output_directory",
        "$project.buildDir"
   ]
}

Here’s the plugin:

<plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>exec-maven-plugin</artifactId>
                                <version>1.2.1</version>
                                <executions>
                                        <execution>
                                                <goals>
                                                        <goal>java</goal>
                                                </goals>
                                        </execution>
                                </executions>
                                <configuration>
                                        <includeProjectDependencies>true</includeProjectDependencies>
                                        <includePluginDependencies>true</includePluginDependencies>
                                        <mainClass>com.abc.efg.func.DocumentGenerator</mainClass>
                                        <executableDependency>
                                                <groupId>com.google.template</groupId>
                                                <artifactId>tool</artifactId>
                                        </executableDependency>
                                        <arguments>
                                                <argument>--api_list</argument>
                                                <argument>metaapi@v1,campaign@v1</argument>
                                                <argument>--output_directory</argument>
                                                <argument>${project.build.directory}</argument>
                                        </arguments>
                                </configuration>
                                <dependencies>
                                        <dependency>
                                                <groupId>com.google.template</groupId>
                                                <artifactId>tool</artifactId>
                                                <version>1.0</version>
                                                <type>jar</type>
                                        </dependency>
                                </dependencies>
                        </plugin>

What errors are you getting?

One thing I spotted was that there’s no dependency for a JavaExec task…

I think (untested) you want something more like:

task executeJava(type:JavaExec) {
   main = "com.abc.efg.func.DocumentGenerator"
   classpath = files('/lib/tool.jar')
   args = [
        "--api_list",
        "metaapi@v1",
        "campaign@v1",
        "--output_directory",
        "$project.buildDir"
   ]
}

I keep getting an error caused by another plugin:

<plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>build-helper-maven-plugin</artifactId>
                                <version>1.7</version>
                                <executions>
                                        <execution>
                                        <id>add-source</id>
                                        <phase>generate-sources</phase>
                                        <goals>
                                                <goal>add-source</goal>
                                        </goals>
                                        <configuration>
                                                <sources>
                                                        <source>${project.build.directory}/generated-sources/templates</source>
                                                </sources>
                                        </configuration>
                                        </execution>
                                </executions>
                        </plugin>

So it’s always complaining it cannot find things in "com.abc.efg.docs.templates ".

Here’s what I did for this plugin:
compile 'org.codehaus.mojo:build-helper-maven-plugin:1.7'
and

sourceSets {
    main {
        java {
            srcDirs 'src/main/java'
            srcDirs "$project.buildDir/generated-sources/templates"
        }
    }
}

Hiya!

What is it that’s “always complaining”?

Do you have a quick failing example you can make public that I could try out?

Sorry I can’t, there is too much code. Basically the generated-sources/templates contains java code that will be used by other code. It’s not generating this directory correctly. Thanks for your help tho.

You do not need the exec-maven-plugin in the dependencies. It does nothing for Gradle. The equivalent is the JavaExec task in gradle, no plugin necessary. What you are creating is a source generator task.

Assuming that you can execute this task already it would generate the sources under $buildDir/generated-sources/templates when the main class runs. You should be able to prove this already.

If this task cannot execute then the JavaExec task probably needs a specific classpath that is not the runtimeClasspath. This to me looks like the classpath that you would define for your DocumentGenerator class to be usable. Such a task specific classpath should be separate from your compile classpath. The following would make such a configuration possible:

configurations {
    documentGenerator
}
dependencies {
    documentGenerator "group:name:version"
}

where “group:name:version” is the maven dependency containing the com.abc.efg.func.DocumentGenerator class. You would then add this to your JavaExec task:

classpath = configurations.documentGenerator

You would have to set the compileJava task to depend on your executeJava task to make this work correctly since you need the generated sources to exist before the compileJava task runs.

compileJava.dependsOn executeJava

I would use a descriptive name relating to what the task generates instead of what it does. Perhaps call it generateDocumentType for whatever type of document code you are generating.