0

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.

rinilnath
  • 136
  • 2
  • 15

2 Answers2

1

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

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • I get only one error. " Could not get unknown property for sqlj in line 29 of this task class. Can you please help ?. I ran the task by giving "gradle sqlj" command – rinilnath May 17 '18 at 12:12
  • You'll need to add the sqlj jar(s) to the buildscript classpath – lance-java May 17 '18 at 12:27
0

Unfortunately I can't see an officially supported sqlj ant task, someone posted source for an ant task here so one option is to wrap that using Gradle's ant integration

Having a look at the source code for the Maven plugin it seems like most of the logic is in the translate method which calls sqlj.tools.Sqlj.statusMain(). I notice there's logic in the Maven task which only updates the files which have changed, this could be done in Gradle via an incremental task

Hope this helps you to get started

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Not sure how to call ant task that is in Java. – rinilnath May 08 '18 at 15:51
  • Any one please say how to call a java ant task in ant's build.xml with example – rinilnath May 09 '18 at 13:46
  • You could put the task source code in `buildSrc/src/main/java` so it's on the buildscript classpath. Then see [here](https://docs.gradle.org/current/userguide/ant.html#sec:using_custom_ant_tasks) for defining an ant task via `ant.taskdef` – lance-java May 09 '18 at 15:58
  • i have successfully compiled the sqlj with the attached ant task file. I am able to run it as a separate ant project and were able to get the sqlj translated. – rinilnath Jun 25 '18 at 14:54
  • But my problem now is, in gradle i am invoking the ant build via import statement – rinilnath Jun 25 '18 at 14:54
  • See [here](https://discuss.gradle.org/t/generate-java-code-from-multiple-wsdls-using-gradle) and [here](https://discuss.gradle.org/t/andromda-plugin-for-gradle) for similar topics where people are trying to generate source code with with ant tasks – lance-java Jun 25 '18 at 14:59
  • Thanks for that, i imported build.xml. But it is showing errors with various Ant classes as symbol not found. It seems that ant jar is not getting reflected in the build though i have given compilegroup and dependencies i could see in .gradle folder of my chocolatey also. What i am missing here ? – rinilnath Jun 26 '18 at 03:15