Aws-cdk: java: Gradle plugin for CDK

Created on 5 Dec 2018  路  6Comments  路  Source: aws/aws-cdk

Similar to the proposal to create a Maven plugin and archetype in #840 I propose to create a Gradle plugin which would make it much more straight forward to work with CDK using a JVM language.

efformedium feature-request in-progress languagjava managemendevenv p2

Most helpful comment

I'm happy to take a stab at this if no one else is working on it?

All 6 comments

I'm happy to take a stab at this if no one else is working on it?

Go for it

@leemorrisdev while this gets develop/merged, what would you suggest as a workaround for gradle-only projects right now? Is there an equivalent to Maven's:

{
  "app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.LambdaCronApp"
}

I'm thinking something in the lines of a lightweight way to call cdk for now, i.e (pseudocode, since I'm not big in Gradle internals):

{
  "app": "gradlew run -Dexec.mainClass=org.umccr.awscdk.IGVAmazonCognitoApp"
}

For more context, I'm testing this out in the following project: https://github.com/umccr/igv/commit/7e160562c87152779f35bc72e4f12523ca40ce63

/cc @reisingerf

@brainstorm and anyone else looking to use gradle with the cdk:

It's relatively easy to use gradle instead of maven with the cdk (for the sample tutorial project at least). The method I used was to perform a gradle init in the project directory (the one containing pom.xml), as this will produce a build.gradle file equivalent to the pom.xml (plus it will add the gradle wrapper).

There were three changes I applied to the resulting build.gradle:

  1. change the maven repository from http://repo.maven.apache.org/maven2 to https://repo.maven.apache.org/maven2
  2. added the application plugin, and configured the main class to be "com.myorg.HelloCdkApp"
  3. remove references to maven publishing plugin (and it's configuration)

My build.gradle ended up as:

plugins {
    id 'java'
    id 'application'
}

repositories {
    mavenLocal()
    maven {
        url = 'https://repo.maven.apache.org/maven2'
    }
}

application {
    mainClassName = 'com.myorg.HelloCdkApp'
}

dependencies {
    implementation 'software.amazon.awscdk:core:1.27.0'
    implementation 'software.amazon.awscdk:s3:1.27.0'
    testImplementation 'junit:junit:4.12'
}

group = 'com.myorg'
version = '0.1'
sourceCompatibility = '1.8'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

I suggest at this point running ./gradlew run to ensure your project builds as expected. If the unit test included in the sample project fails (as it did for me) you can remove the test class to get the example building.

You can then edit cdk.json to be:

{
  "app": "./gradlew run"
}

and then execute cdk deploy.

Once this works you can mvn clean and then remove maven files (pom.xml etc) from your project.

Thanks @geoffweatherall ! I went with your suggestions in https://github.com/umccr/igv/commit/dbd7ca5d78ab58cd1778e7f3f30e9ae79e6ab00a, the classes seem to compile (generate .class files under build) but so far it got me here:

% cdk deploy

BUILD SUCCESSFUL in 868ms
ENOENT: no such file or directory, open 'cdk.out/manifest.json'
% cdk synth

BUILD SUCCESSFUL in 831ms
ENOENT: no such file or directory, open 'cdk.out/manifest.json'

I'll keep investigating...

/cc @reisingerf

Nevermind, easy fix: https://github.com/umccr/igv/commit/385f7c3691950ec45448340acc9f32762e622487 ... cruising with CDK Java now ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

peterdeme picture peterdeme  路  3Comments

eladb picture eladb  路  3Comments

v-do picture v-do  路  3Comments

kawamoto picture kawamoto  路  3Comments

EduardTheThird picture EduardTheThird  路  3Comments