How can I mark with `final` classes generated by xjc? - java

I'm using xjc in gradle as follows:
task xjc(type: JavaExec) {
inputs.files(schemaDir)
outputs.dir(generatedSrcDir)
main = 'com.sun.tools.xjc.Driver'
classpath = configurations.xjc
jvmArgs '-Djavax.xml.accessExternalSchema=all'
args '-quiet',
'-d',
"$generatedSrcDir",
'-classpath',
"$configurations.xjc.asPath",
'-disableXmlSecurity',
'-extension',
'-npa',
'-no-header',
'-enableIntrospection',
'-Xxew',
'-Xxew:instantiate lazy',
'-Xcomments',
'-Xcomments-mode=replace',
'-Xequals',
'-Xhashcode',
'-Xtostring',
"$schemaDir",
'-b',
"$schemaDir"
doFirst {
// Create directory for generated sources if not exists
mkdir(generatedSrcDir)
println "Generating JAXB sources with XJC version $jaxbVersion ..."
}
}
I need to know if I could mark with final all classes suitable?
P.S. Point of final here is to preserve the equals contract for generated classes.

Related

Generate classes from wsdl when path is /src/client/java

I am trying to generate classes from a wsdl file. My source root path does not includes main but client.
The root is src/client/java
my build.gradle:
plugins {
id "com.github.bjornvester.wsdl2java" version "1.2"
}
dependencies {
implementation "org.apache.cxf:cxf-spring-boot-starter-jaxws:3.5.3"
}
wsdl2java {
wsdlDir = file("src/client/resources/wsdl")
includes = [
"wsdl/QueryService.wsdl"
]
}
When I build the project, the build/generated/sources/wsdl2java/java[main] (it looks like this) is empty.
There is no error.
what am I doing wrong?

cxf-codegen-plugin in Gradle classpath in jax-ws-catalog.xml

Firstly idea was to generate java classes without internet connection, so I decided use jax-ws-catalog.xml file to rewrite uri on resource/local directory.
I defined location of xsd files by using jax-ws-catalog.xml with classpath.
But every compilation is wrong and catching error:
WARN org.apache.cxf.resource.URIResolver - Resource classpath:/local/xml.xsd was not found in the classloaders.
WSDLToJava Error: org.apache.ws.commons.schema.XmlSchemaException: Unable to locate imported document at 'http://www.w3.org/2001/xml.xsd', relative to ...{my file}...
build.gradle
plugins {
id 'java'
id 'java-library'
id "io.mateo.cxf-codegen" version "1.0.2"
}
import io.mateo.cxf.codegen.wsdl2java.Wsdl2Java
ext {
artifactId = 'soap-adapter'
}
jar {
enabled = true
}
bootJar {
enabled = false
}
dependencies {
implementation "javax.xml.ws:jaxws-api:${jaxwsApiVersion}"
implementation "javax.jws:javax.jws-api:${javaxJwsApiVersion}"
implementation "com.sun.xml.messaging.saaj:saaj-impl:${sunSaajImplVersion}"
implementation "org.apache.cxf:cxf-rt-frontend-jaxws:${apacheCxfVersion}"
implementation "org.apache.cxf:cxf-rt-transports-http:${apacheCxfVersion}"
implementation "org.apache.cxf:cxf-rt-ws-security:${apacheCxfVersion}"
cxfCodegen "jakarta.xml.ws:jakarta.xml.ws-api"
cxfCodegen "jakarta.annotation:jakarta.annotation-api"
cxfCodegen "ch.qos.logback:logback-classic"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// Common properties of java generation from wsdl.
tasks.withType(Wsdl2Java).configureEach {
toolOptions {
markGenerated.set(true)
outputDir.set(file("${buildDir}/generated"))
addToMainSourceSet.set(true)
wsdl.set(file("${projectDir}/src/main/resources/wsdl/myWsdl.wsdl"))
catalog.set("${projectDir}/src/main/resources/META-INF/jax-ws-catalog.xml")
}
}
compileJava.dependsOn wsdl2java
jax-ws-catalog.xml
<?xml version="1.0"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<rewriteURI uriStartString="http://www.w3.org/2001/xml.xsd" rewritePrefix="classpath:/local/xml.xsd"/>
</catalog>
I think that classpath is determined wrong.
I discovered the next:
At the programm compilation jax-ws-catalog.xml using only relative paths from itself.
At the runtime you can use classpath.
And jax-ws library using resurce/META-INF/jax-ws-catalog.xml by default.
So I just duplicated this jax-ws-catalog.xml into next directories with different properties:
resource/jax-ws-catalog.xml
<?xml version="1.0"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
<rewriteURI uriStartString="http://" rewritePrefix="local/"/>
<rewriteURI uriStartString="https://" rewritePrefix="local/"/>
</catalog>
resource/META-INF/jax-ws-catalog.xml
<?xml version="1.0"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
<rewriteSystem systemIdStartString="http://www.w3.org/2006/03/addressing" rewritePrefix="classpath:local/www.w3.org/2006/03/addressing"/>
<rewriteSystem systemIdStartString="http://www.w3.org/2005/08/addressing" rewritePrefix="classpath:local/www.w3.org/2005/08/addressing"/>
</catalog>
In build.gradle I changed the next
tasks.withType(Wsdl2Java).configureEach {
toolOptions {
markGenerated.set(true)
outputDir.set(file("${buildDir}/generated"))
addToMainSourceSet.set(true)
wsdl.set(file("${projectDir}/src/main/resources/wsdl/myWsdl.wsdl"))
catalog.set("${projectDir}/src/main/resources/jax-ws-catalog.xml")
}
}

Cannot add task ':jacocoTestReport' as a task with that name already exists

I am trying to add the following task so that I can get some coverage data in my java + kotlin project (for what it is worth, this is a gradle project)... but I get the following error :
"Cannot add task ':jacocoTestReport' as a task with that name already exists"
Here is the actual task I am trying to add :
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports for Debug build"
reports {
xml.enabled = true
html.enabled = true
}
// what to exclude from coverage report
// UI, "noise", generated classes, platform classes, etc.
def excludes = [
'**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*Fragment.*',
'**/*Activity.*'
]
// generated classes
classDirectories = fileTree(
dir: "$buildDir/intermediates/classes/debug",
excludes: excludes
) + fileTree(
dir: "$buildDir/tmp/kotlin-classes/debug",
excludes: excludes
)
// sources
sourceDirectories = files([
android.sourceSets.main.java.srcDirs,
"src/main/kotlin"
])
executionData = files("$buildDir/jacoco/testDebugUnitTest.exec")
}
Now, the issue I am confused about here, is that I can't find another class of this name anywhere... so perhaps there is something funky going on? I have tried googling this, but haven't really been able to find anything which truly helps me solve the problem.
All help greatly appreciated. I realize this is not a java or kotlin specific problem - but since it is a joint java + kotlin project, I thought I would tag both in this question, in case there is some nuanced issue that somebody else has seen.
Assuming you're already applying the Jacoco Gradle plugin, then yes, it already defines a task called jacocoTestReport, hence the error.
All you need to do is define your specific settings as per the documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_report_configuration
an example is below:
jacocoTestReport {
dependsOn "testDebugUnitTest"
reports {
xml.enabled = true
html.enabled = true
}
}
Most of the other configuration items you've listed belong in the 'jacoco' configuration block.
https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_specific_task_configuration

How to create a pom.xml file with Gradle using variables?

I want to write a Gradle function, which creates a pom.xml file with data (groupId, artifactId, version) passed as arguments.
I created following script:
apply plugin: 'maven'
apply plugin: 'java'
def createMainPom(mainDir, groupId, artifactId, version)
{
pom
{
project
{
groupId '$groupId' // Error
artifactId '$artifactId'
version '$version'
packaging 'pom'
name 'New app'
modules
{
module
{
name 'app'
}
module
{
name 'integration-tests'
}
}
}
}.writeTo('$mainDir/pom.xml');
}
[...]
When I run it, I get the error at the position marked with // Error above:
> No signature of method: java.lang.String.call() is applicable for argument typ
es: (java.lang.String) values: [a]
Possible solutions: wait(), any(), wait(long), split(java.lang.String), find(jav
a.lang.String), count(java.lang.String)
How can I fix this?
The plugin documentation states:
Note: groupId, artifactId, version, and packaging should always be set directly on the pom object.
Does moving those properties up a level, out of project, make a difference?
For strings variables resolution use double quotes:
groupId "$groupId"
artifactId "$artifactId"
version "$version"
...
}.writeTo("$mainDir/pom.xml");

buildr: package dependencies into a single jar

I have a java project that is built with buildr and that has some external dependencies:
repositories.remote << "http://www.ibiblio.org/maven2"
repositories.remote << "http://packages.example/"
define "myproject" do
compile.options.target = '1.5'
project.version = "1.0.0"
compile.with 'dependency:dependency-xy:jar:1.2.3'
compile.with 'dependency2:dependency2:jar:4.5.6'
package(:jar)
end
I want this to build a single standalone jar file that includes all these dependencies.
How do I do that?
(there's a logical followup question: How can I strip all the unused code from the included dependencies and only package the classes I actually use?)
This is what I'm doing right now. This uses autojar to pull only the necessary dependencies:
def add_dependencies(pkg)
tempfile = pkg.to_s.sub(/.jar$/, "-without-dependencies.jar")
mv pkg.to_s, tempfile
dependencies = compile.dependencies.map { |d| "-c #{d}"}.join(" ")
sh "java -jar tools/autojar.jar -baev -o #{pkg} #{dependencies} #{tempfile}"
end
and later:
package(:jar)
package(:jar).enhance { |pkg| pkg.enhance { |pkg| add_dependencies(pkg) }}
(caveat: I know little about buildr, this could be totally the wrong approach. It works for me, though)
I'm also learning Buildr and currently I'm packing Scala runtime with my application this way:
package(:jar).with(:manifest => _('src/MANIFEST.MF')).exclude('.scala-deps')
.merge('/var/local/scala/lib/scala-library.jar')
No idea if this is inferior to autojar (comments are welcome), but seems to work with a simple example. Takes 4.5 minutes to package that scala-library.jar thought.
I'm going to use Cascading for my example:
cascading_dev_jars = Dir[_("#{ENV["CASCADING_HOME"]}/build/cascading-{core,xml}-*.jar")]
#...
package(:jar).include cascading_dev_jars, :path => "lib"
Here is how I create an Uberjar with Buildr, this customization of what is put into the Jar and how the Manifest is created:
assembly_dir = 'target/assembly'
main_class = 'com.something.something.Blah'
artifacts = compile.dependencies
artifacts.each do |artifact|
Unzip.new( _(assembly_dir) => artifact ).extract
end
# remove dirs from assembly that should not be in uberjar
FileUtils.rm_rf( "#{_(assembly_dir)}/example/package" )
FileUtils.rm_rf( "#{_(assembly_dir)}/example/dir" )
# create manifest file
File.open( _("#{assembly_dir}/META-INF/MANIFEST.MF"), 'w') do |f|
f.write("Implementation-Title: Uberjar Example\n")
f.write("Implementation-Version: #{project_version}\n")
f.write("Main-Class: #{main_class}\n")
f.write("Created-By: Buildr\n")
end
present_dir = Dir.pwd
Dir.chdir _(assembly_dir)
puts "Creating #{_("target/#{project.name}-#{project.version}.jar")}"
`jar -cfm #{_("target/#{project.name}-#{project.version}.jar")} #{_(assembly_dir)}/META-INF/MANIFEST.MF .`
Dir.chdir present_dir
There is also a version that supports Spring, by concatenating all the spring.schemas

Categories

Resources