Search code examples
javamavengradlesqlj

Is there any Gradle -Sqlj plugin available to do sqlj translation as we do using maven sqlj plugin?


I had converted my maven build project to gradle and now one of the plugins that we use in maven is SQLj plugin. The sqlj plugin has additional configuration that takes the source (.sqlj) files from specific folder path and then compiles them to .java and .ser specific target directory.

I am not sure on configuring this in the gradle build. Can you please help me how to write this configurations in build.gradle file .

I just included the dependency line equivalent to gradle that i got from mvncentral.

// https://mvnrepository.com/artifact/org.codehaus.mojo/sqlj-maven-plugin compile group: 'org.codehaus.mojo', name: 'sqlj-maven-plugin', version: '1.3'

but it just not compiling the new .sqlj from specific file location. I had included the lines like applyplugin: java and maven.

Let me know if any additional thing need to be done.


Solution

  • You could use the source code from the maven plugin as a guide to develop a Gradle task. Here's a simple starting point

    class SqljTask extends DefaultTask {
        @Input
        String encoding
        @Input
        List<String> additionalArgs = []
        @InputDirectory
        File sqljDir
        @OutputDirectory
        File generatedJava
        @OutputDirectory
        File generatedResources
    
        @TaskAction
        void generate() {
    
            project.mkdir generatedJava
            project.mkdir generatedResources
    
            project.fileTree(sqljDir).visit { FileVisitDetails fvd ->
               if (!fvd.directory) {
                   List<String> sqljArgs = []
                   sqljArgs << "-dir=$generatedJava"
                   sqljArgs << "-d=$generatedResources"
                   sqljArgs << "-encoding=$encoding"
                   sqljArgs << "-compile=false"
                   sqljArgs << fvd.file.absolutePath
                   sqljArgs.addAll(additionalArgs)
                   int result = sqlj.tools.Sql.statusMain(sqljArgs as String[])
                   if (result != 0) throw new RuntimeException("Can't translate $fvd.file ($returnCode)"   
               }            
            }
        }
    }
    

    Usage in a build.gradle

    apply plugin: 'java'
    
    task sqlj(type: SqljTask) {
        encoding = 'UTF-8'
        additionalArgs = ['-status']
        sqljDir = file('src/main/sqlj')
        generatedJava = file("$buildDir/sqlj/java")
        generatedResources = file("$buildDir/sqlj/resources")
    }
    
    compileJava.dependsOn sqlj
    
    sourceSets {
        main {
            java { 
                srcDir sqlj.generatedJava
            }
            resources {
                srcDir sqlj.generatedResources
            }
        }
    }
    

    Note: This task will only run if a task input / output has changed since the last successful run. So it will be considered UP-TO-DATE if nothing has changed since the last build