Issues with converting a multi-module Maven project to Gradle

I am learning Gradle. I come from Maven. I am investigating how easy it is to convert a large multi-module Maven project to Gradle. I have found some interesting things.

ONE: It appears that “gradle init --type=pom” does not pay attention to the pluginManagement section of the pom. Specifically, I was looking to see that Gradle would use the settings for the maven-compiler-plugin that are set in the pluginManagement section into the appropriate build.gradle area.

I first tried a simple conversion using the following project structure. Simple is a simple class that uses the diamond operator so it requires source = 1.7 to compile.

.
├── pom.xml
└── src
    └── main
        └── java
            └── Simple.java

I then ran “gradle init --type=pom” to convert the pom to build.gradle. The result was as I expected. Good. Here is the source pom.xml and the resulting build.gradle file:

Source pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jhines02.gradle-testing</groupId>
  <artifactId>simple-compiler-plugin-settings</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArgument>-Xlint:all</compilerArgument>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Created build.gradle:

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.jhines02.gradle-testing'
version = '1.0-SNAPSHOT'

description = """"""

sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
     maven { url "http://nexus-server/nexus/content/repositories/snapshots" }
     maven { url "http://repo.maven.apache.org/maven2" }
}

However, if I have a multi-module project in which the compiler source and target settings are defined in the pluginManagement element, then “gradle init” does not get it right.

Here is the structure of a similar multi-module Maven project. Only now the compiler settings are defined in the pluginManagement element:

.
└── top
    ├── pom.xml
    └── project
        ├── pom.xml
        └── src
            └── main
                └── java
                    └── Simple.java

Source top/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jhines02.gradle-testing.multi-module-compiler-plugin-settings</groupId>
  <artifactId>top</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>project</module>
  </modules>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <compilerArgument>-Xlint:all</compilerArgument>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Source project/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <parent>
    <groupId>com.jhines02.gradle-testing.multi-module-compiler-plugin-settings</groupId>
    <artifactId>top</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>project</artifactId>
  <packaging>jar</packaging>
</project>

Resulting top/build.gradle:

allprojects  {
  apply plugin: 'maven'

  group = 'com.jhines02.gradle-testing.multi-module-compiler-plugin-settings'
version = '1.0-SNAPSHOT'
}

subprojects {
  apply plugin: 'java'
  sourceCompatibility = 1.5
  targetCompatibility = 1.5

  repositories {
    mavenLocal()

    maven { url "http://nexus-server/nexus/content/repositories/snapshots" }
    maven { url "http://repo.maven.apache.org/maven2" }
  }
}

TWO: It appears that “gradle init --type=pom” does not handle converting a child pom without trying to convert the whole multi-module project. Perhaps this is intentional. I’m thinking that the use case for this is trying to convert just part of a large multi-module Maven project.

Using the above multi-module Maven project, I just tried to convert the project/pom.xml. (Note, I first deleted the build.gradle file that was generated previously during ONE). I got the following error:

$ gradle init --type=pom

FAILURE: Build failed with an exception.

* What went wrong:
Task 'init' not found in project ':project'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 11.47 secs

I’m assuming that the error is due to trying to convert a child pom without first converting the parent pom. However, the error doesn’t say that. Do you think trying to convert just part of a large multi-module Maven project is a viable use-case?

Note, I am using Gradle 2.4.