Generate a single JAR files with multiple JARs and wsdl files combined together

Viewed 76

I am trying to generate a JAR (salesforce-soap-connection.jar) file that I want to use as a dependency (library) in my project. Currently, I generate this jar file using the command below and then push the generated jar file in my repo.

java -classpath ./sf/force-wsc-45.0.0.jar:./sf/js-1.7R2.jar:./sf/ST4-4.3.1.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/tools.jar:./sf/antlr-4.7.2-complete.jar com.sforce.ws.tools.wsdlc ./sf/enterprise.wsdl ./sf/salesforce-soap-connection.jar

Basically, I want to do this using Gradle. (Instead of running the above command, do exactly what the command does using Gradle)

The jar files that are used in the above command should be downloaded from maven repository and only enterprise.wsdl will be present in project/src/main/resources folder.

The generated jar file should be kept in project/lib folder.

This is my build.gradle file. Please suggest what to do next…

plugins {
    id 'scala'
}
    
group 'org.example'
version '1.0-SNAPSHOT'
    
repositories {
    mavenCentral()
}
    
dependencies {
    implementation 'org.scala-lang:scala-library:2.11.8'
    implementation 'joda-time:joda-time:2.1'
    implementation 'org.joda:joda-convert:2.1.1'
    implementation 'org.slf4j:slf4j-api:1.7.36'
    implementation 'org.apache.logging.log4j:log4j-api:2.6.2'
    implementation 'org.apache.logging.log4j:log4j-core:2.6.2'
    implementation 'com.google.cloud:google-cloud-storage:1.52.0'
    implementation 'com.google.cloud:google-cloud-bigquery:1.110.0'
    implementation 'org.jooq:jooq:3.12.0'
    implementation 'org.jooq:jooq-scala_2.11:3.10.5'
    implementation 'com.google.apis:google-api-services-bigquery:v2-rev459-1.25.0'
    implementation 'org.json:json:20180813'
    implementation 'com.google.code.gson:gson:2.8.3'
    implementation 'org.yaml:snakeyaml:1.25'
    implementation 'com.github.tototoshi:scala-csv_2.11:1.3.6'
    implementation 'org.json4s:json4s-native_2.11:3.6.10'
    implementation 'org.apache.commons:commons-dbcp2:2.0.1'
    implementation 'commons-io:commons-io:2.6'
    implementation fileTree('lib')
}
    
jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes "Main-Class": "com.org.loader.main"
    }
    
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
1 Answers

I was able to solve the issue with gradle JavaExec type task.

JavaExec Executes a Java application in a child process.

Similar to Exec, but starts a JVM with the given classpath and application class.

I have used toCopy as a configuration so that it is only salesForceJarGenerate task and not included in project external dependencies.

transitive = false is used to stop given dependency to not download or exclude its transitive dependencies.

In salesForceJarGenerate task:-

classpath option is to include dependencies/jars that are needed to be provided for the soap-connector.jar

main option is to provide the main class that will convert .ml files to java files.

args is the arguments.

agrs "{location of your enterprise.xml file}" , "output location of your jar file" , "location where the intermediate java files will be stored"

More on this can be found on official documentation here!

here is my final build.gradle


    plugins {
        id 'scala'
       
    }
    
    group 'org.example'
    version '1.0-SNAPSHOT'
    
    
    repositories {
        mavenCentral()
    }
    
    
    
    dependencies {
        implementation 'org.scala-lang:scala-library:2.11.8'
        implementation 'joda-time:joda-time:2.1'
        implementation 'org.joda:joda-convert:2.1.1'
        implementation 'org.slf4j:slf4j-api:1.7.36'
        implementation 'org.slf4j:slf4j-simple:2.0.0'
        implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.12.1'
    
        implementation 'org.apache.logging.log4j:log4j-api:2.6.2'
        implementation 'org.apache.logging.log4j:log4j-core:2.6.2'
        implementation 'com.google.cloud:google-cloud-storage:1.52.0'
        implementation 'com.google.cloud:google-cloud-bigquery:1.110.0'
        implementation 'org.jooq:jooq:3.12.0'
        implementation 'org.jooq:jooq-scala_2.11:3.10.5'
        implementation 'com.google.apis:google-api-services-bigquery:v2-rev459-1.25.0'
        implementation 'org.json:json:20180813'
        implementation 'com.google.code.gson:gson:2.8.3'
        implementation 'org.yaml:snakeyaml:1.25'
        implementation 'com.github.tototoshi:scala-csv_2.11:1.3.6'
        implementation 'org.json4s:json4s-native_2.11:3.6.10'
        implementation 'org.apache.commons:commons-dbcp2:2.0.1'
        implementation 'commons-io:commons-io:2.6'
        implementation 'com.force.api:force-wsc:54.0.0'
    
    
        implementation fileTree('lib')
    
    }
    configurations {
        toCopy
    }
    dependencies {
        toCopy('com.force.api:force-wsc:45.0.0') {
            transitive = false
        }
        toCopy('rhino:js:1.7R2') {
            transitive = false
        }
        toCopy('org.antlr:ST4:4.3.1') {
            transitive = false
        }
        toCopy('org.antlr:antlr-complete:3.5.2') {
    
            transitive = false
        }
        toCopy('jdk.tools:jdk.tools:1.8') {
            transitive = false
        }
    }
    
    
    //generates salesforce-soap-connection.jar with the given enterprise.xml file
    task salesForceJarGenerate(type: JavaExec) {
        classpath = configurations.toCopy
        main = "com.sforce.ws.tools.wsdlc"
        args "${projectDir}/src/main/resources/enterprise.xml", "${projectDir}/lib/salesforce-soap-connection.jar", "$buildDir/salesforce-gen/"
    
    }
    
    jar {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        manifest {
            attributes "Main-Class": "com.org.loader.main"
        }
        
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
    compileScala.dependsOn salesForceJarGenerate

Now the generated jar file can be added as a dependency by adding the below in dependencies block.

implementation fileTree("location(directory) of generated jar file")
Related