How can I run WSDLToJava from a build.gradle file? - java

I am successfully able to run the WSDLToJava class from the command line to generate JaxB classes from a WSDL.
java -Xmx128M
-cp "C:\cxf\apache-cxf-3.1.6\lib\cxf-manifest.jar;
C:\java\jdk1.7.0_80\lib\tools.jar"
-Djava.util.logging.config.file="C:\cxf\apache-cxf-3.1.6
\etc\logging.properties"
org.apache.cxf.tools.wsdlto.WSDLToJava
-d generated -frontend jaxws21 -b C:\Project\jaxb-bindings.xml
C:\Project\Service.wsdl
How can I run the same command line from a "build.gradle" file? I am completely new to Gradle.
Thank you in advance for your help.
Pete

While there are some Gradle plugins that implement this, I think it's just as easy to make the call directly via a JavaExec task. Add the following to your build.gradle:
ext.cxfVersion = "3.1.6"
configurations {
wsdl2java
}
dependencies {
wsdl2java("org.apache.cxf:cxf-tools-wsdlto-core:${cxfVersion}")
wsdl2java("org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:${cxfVersion}")
wsdl2java("org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:${cxfVersion}")
}
task wsdl2java(type: JavaExec) {
main = "org.apache.cxf.tools.wsdlto.WSDLToJava"
classpath = configurations.wsdl2java
// Uncomment to add JVM arguments if necessary (e.g. for cert-based security)
//jvmArgs = [
// "-Djavax.net.ssl.keyStore=${keystorePath}",
// "-Djavax.net.ssl.keyStorePassword=${keystorePassword}"
//]
args = [
"-d", "src/gen/java",
"example.wsdl"
// Uncomment and remove the previous line to run for multiple WSDL files
//"-wsdlList, "wsdls.txt"
]
}
// If you want to be able to debug the wsdl2java task from Eclipse, add the following
//eclipse {
// classpath {
// plusConfiguration += [configurations.wsdl2java]
// }
//}
Execute the task by running gradlew wsdl2java.

Related

Android studio beginner :( Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0

Hi, Im new to coding and android studio. I wrote just a simple PSVM SOUT message and the code is correct but this is what it shows in my terminal (BTW this is a fresh new install) :
Initialization script 'C:\Users\Dom\AppData\Local\Temp\MainActivity_main__.gradle' line: 20
* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:MainActivity.main()'.
> SourceSet with name 'main' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more
log
output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings
The gradle file that it is referring to looks like this:
def gradlePath = ':app'
def runAppTaskName = 'MainActivity.main()'
def mainClass = 'com.example.myapplication.MainActivity'
def javaExePath = 'S:/Program files (x86)/androidStudio/jre/bin/java.exe'
def _workingDir = 'C:/Users/Dom/AndroidStudioProjects/MyApplication'
def sourceSetName = 'main'
def javaModuleName = null
allprojects {
afterEvaluate { project ->
if(project.path == gradlePath && project?.convention?.findPlugin(JavaPluginConvention)) {
project.tasks.create(name: runAppTaskName, overwrite: true, type: JavaExec) {
if (javaExePath) executable = javaExePath
classpath = project.sourceSets[sourceSetName].runtimeClasspath
main = mainClass
if(_workingDir) workingDir = _workingDir
standardInput = System.in
if(javaModuleName) {
inputs.property('moduleName', javaModuleName)
doFirst {
jvmArgs += [
'--module-path', classpath.asPath,
'--module', javaModuleName + '/' + mainClass
]
classpath = files()
}
}
}
}
}
}
I have tried to update the gradle version but when I updated it to 6 it said it wasn't compatible with gradle 7. It says to run with --stacktrace option but what is it referring to?
where do I run this line of code?
You telling gradle to use the sourceset main which is normaly default, but do you really have a folder called main ?
I would expect you have this kind of folder ?
C:/Users/Dom/AndroidStudioProjects/MyApplication/src/main
Which version of Java do you use ?

Gradle: set java virtual machine (jvm) stack size for task run? (eg. pass -Xss80m to jvm)

When I use Gradle to execute my Java program, I'd like to manually set the stack size. Executing java from command line, it would be something like java -Xss80m, but I'm new to Gradle and struggling a bit to find the best or standard way to do this with a build script...
Excerpt of my Gradle build script is below:
task run(type: JavaExec) {
classpath += sourceSets.main.runtimeClasspath
main = 'loadBuybackRowsIntoSQL'
}
According to docs, jvmArgs is what you need:
task run(type: JavaExec) {
...
jvmArgs '-Xss80m' // will add this argument to list
// or
jvmArgs = ['-Xss80m'] // will replace arguments list
}

How do I add default JVM arguments with Gradle

I need to add default JVM options to my jar, when build with Gradle.
From the documentation I got that I have to set:
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
I have not much experience with Gradle and the developer that wrote the build.gradle file wrote it different from what most websites give as examples.
Here is the build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
version = '0.1'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'placeholder'
}
task release(type: Jar) {
manifest {
attributes("Implementation-Title": "placeholder",
"Implementation-Version": version,
'Main-Class': 'placeholder.Application')
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
I don't know where to put the arguments. I tried putting them in different locations, but I always get:
A problem occurred evaluating root project 'placeholder'.
> No such property: applicationDefaultJvmArgs for class: org.gradle.api.tasks.bundling.Jar_Decorated
Much Thanks,
Jhonny
From the top of my head I can think of 2 options:
Option1: Do what #Ethan said, it'll likely work:
package placeholder;
//your imports
public class Application{
static {
System.getProperties().set("javafx.embed.singleThread", "true");
}
// your code
public static void main(String... args){
//your code
}
}
Option 2: Use the application plugin + default jvm values
build.gradle:
apply plugin: 'application'
//your code
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
Now you can run your code 2 ways:
From gradle
$gradle run
From distribution(script). from the generated script that the application plugin will provide:
$gradle clean build distZip
Then gradle will generate a zip file somewhere under ${your.projectdir}/build. Find the zip then unzip it and under /bin you'll find a ${yourproject}.bat and ${yourproject} executables. One is for Linux/mac/unix (${yourproject}) the other one is for windows (${yourproject.bat})
Option 3 (Android Developer): Use gradle.properties to set jvm argument
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# You can setup or customize it according to your needs and combined with the above default value.
org.gradle.jvmargs=-Djavafx.embed.singleThread=true
For more info on how to use gradle build environment on docs.gradle.org
applicationDefaultJvmArgs is provided by the Application plugin. So if you apply that plugin, the error would probably go away, and you should be able to execute the program by issuing gradle run once you set the mainClassName property to the fully qualified class name, the main method of which you want to invoke.
Using a local gradlew is the easiest.
Just append to DEFAULT_JVM_OPTS.
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m" "-XX:+AllowRedefinitionToAddDeleteMethods"'
you can use command-line with gradle task:
class AppRun extends JavaExec {
private boolean withDebug
#Option(option = "with-debug", description = "enable debug for the process. ")
public void setDebugMode(boolean debug) {
this.withDebug = debug
}
public boolean getDebugMode() {
return this.withDebug
}
}
task run(type: AppRun) {
}
then run task with options
gradle run --with-debug
set this to Your java main Class.
static {
System.setProperty("nashorn.args", "--no-deprecation-warning");
}

how to pass dynamic input pararm to gradle java exec task?

I have this Gradle task:
task resources_cleaner_bl(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
systemProperties['cleanTarget'] = 'bl'
main = "com.m.ResourcesCleanerRunner"
}
How can I edit this such that 'bl' will be entered dynamically when I run this gradle task.
You can simply eliminate the configuration systemProperties['cleanTarget'] = 'bl' by setting the system property from the command line using the -D flag, i.e. running gradlew resources_cleaner_bl -DcleanTarget=bl.
You can read more about project and system properties in Gradle user guide here.

How to pass JVM options from bootRun

I'm developing simple Spring web application that communicates with remote host and I would like to test it locally behind corporate proxy.
I use "Spring Boot" gradle plugin and the question is how can I specify proxy settings for JVM?
I have try several ways to do it:
gradle -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080 bootRun
export JAVA_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
export GRADLE_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
But it seems like none of them work - "NoRouteToHostException" throws in "network" code.
Also, I have added some extra code to debug JVM start arguments:
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (String arg: arguments) System.out.println(arg);
And only one argument was printed: "-Dfile.encoding=UTF-8".
If I set system property in code:
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
Everything works just fine!
Original Answer (using Gradle 1.12 and Spring Boot 1.0.x):
The bootRun task of the Spring Boot gradle plugin extends the gradle JavaExec task. See this.
That means that you can configure the plugin to use the proxy by adding:
bootRun {
jvmArgs = "-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"
}
to your build file.
Of course you could use the systemProperties instead of jvmArgs
If you want to conditionally add jvmArgs from the command line you can do the following:
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs project.jvmArgs.split('\\s+')
}
}
gradle bootRun -PjvmArgs="-Dwhatever1=value1 -Dwhatever2=value2"
Updated Answer:
After trying out my solution above using Spring Boot 1.2.6.RELEASE and Gradle 2.7 I observed that it was not working as some of the comments mention.
However, a few minor tweaks can be made to recover the working state.
The new code is:
bootRun {
jvmArgs = ["-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"]
}
for hard-coded arguments, and
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs = (project.jvmArgs.split("\\s+") as List)
}
}
for arguments provided from the command line
bootRun {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
This should pass all JVM options to the app started via bootRun.
In gradle build script, define systemProperties for run task.
//to provide the properties while running the application using spring-boot's run task
run {
systemProperties['property name'] = 'value'
}
and gradle run should accept this value.
Or define a project level property as mentioned in
http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run
#marvin, thanks for your post it was very helpful.
Sharing how I used it:
test {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:
//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Doing this with gradle required that the system property provided at the time of running gradle build, shown here,
gradle build -Ddeep.test.run=true
was indeed passed through to the tests.
Hope this helps others trying out this approach for running tests conditionally.
bootRun {
args = ['myProgramArgument1', 'myProgramArgument2']
}
Using jvmArgs may cause JVM start issues. Using args allows you to pass your custom program arguments
It seems to work:
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
I got into a similar problem, bootRun needed some parameters but I wouldn't feel like modifying bootRun as I want to keep some flexibility and stick to standard bootRun behaviour. My suggestion is to add some custom tasks (let's say bootRunDev, bootRunProxy) that extends bootRun, as described in the following code snippet
task bootRunPxy(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'http.proxyHost', 'xxxxx'
systemProperty 'http.proxyPort', 'yyyyy'
}
}
I don't have an environment to exercise the script but I used this approach to pass profile to spring using the property spring.profiles.active.
Credits should go to Karol Kaliński
It's worth mentioning, here, that some systems that use Gradle and Spring Boot are starting JVM outside of build.gradle, e.g. in a Dockerfile.
It's not pointless to mention this on a thread specifically about bootRun! I wound up here because this particular post is a magnet for searches about jvm options in the context of a Spring Boot app compiled / run under gradle. (All the advice I found for adding java.net.http.httpclient logging said "add it to bootRun's jvmArgs". Nothing happened, though.
So, if you happen to run your gradle-built Spring Boot app from a Docker container, you'll want to add your JVM params to an env var in your project's Dockerfile, like so, e.g.-
...
ENV JAVA_OPTS "${JAVA_OPTS} \
-server \
-Duser.timezone=UTC \
-XX:InitialRAMPercentage=50 \
-XX:MaxRAMPercentage=50 \
-Djavax.net.ssl.trustStorePassword=elvislives \
-Djavax.net.ssl.trustStoreProvider=BCFIPS \
-Djavax.net.ssl.trustStoreType=BCFKS \
-Djdk.internal.httpclient.debug=true \
-Djava.util.logging.manager=org.apache.logging.log4j2.jul.LogManager \
-Djdk.httpclient.HttpClient.log=errors,requests,headers,frames[:control:data:window:all..],content,ssl,trace,channel \
"
...
ENTRYPOINT java ${JAVA_OPTS} -cp app:app/lib/* com.mygreatcompany.theapp
For development as Docker Container add to run_script.sh as JAVA_OPTS
JAVA_OPTS="-XX:+UseG1GC
-Xms512m
-Xmx2048m
--add-opens java.base/java.util=ALL-UNNAMED -Dspring.profiles.active=$PROFILE,discovery"

Categories