According to Oracle documentation the schemagen tool is removed from the JDK as part of JEP 320 (http://openjdk.java.net/jeps/320).
That JEP points to Maven artifacts that now supply the missing tools. The coordinates of the artifacts are wrong in the JEP, updated coordinates are found in an answer to this question:
Which artifacts should I use for JAXB RI in my Maven project?
What is missing however is how to invoke the tools. There are shell scripts pointed to in the JEP that are in the JAXB-RI Git repository. However those scripts remain undocumented and difficult to invoke. The build instructions in that git repo indicated it is built with a standard "mvn clean install", however that does not produce an output structure that matches the 'bin' folder used in the documentation here: https://javaee.github.io/jaxb-v2/doc/user-guide/ch04.html#tools-schemagen
Ideally I would like to run schemagen from Gradle, avoiding the shell scripts as they are not obtained from the maven dependency.
My current attempt, adapted from a working version that called the old schemagen.exe, looks like this:
(There is more in the 'real' build.gradle file to specify my application's dependencies, etc.)
configurations {
schemagenTool
}
dependencies {
schemagenTool "org.glassfish.jaxb:jaxb-jxc:${jaxbVersion}"
}
task schemaGen(type: Exec, dependsOn: [compileJava,configurations.schemaGenTool]) {
workingDir projectDir
executable 'java'
doFirst {
file('build/Schemas').mkdirs()
args '--module-path', "${configurations.schemaGenTool.asPath}",
'-m', 'jaxb.jxc/com.sun.tools.jxc.SchemaGeneratorFacade',
// Note: automatic module name is NOT com.sun.tool.jxc
// as documented
// Args to schemagen (these worked for the schemagen.exe)
'-d', 'build/Schemas',
'-classpath', "${compileJava.destinationDir}${File.pathSeparator}${configurations.compile.asPath}",
"src/main/java/path/to/my/ModelClass.java"
//println "\nschemagen: ${args.join(' ')}\n"
}
doLast {
// Above creates "build/Schemas/schema1.xsd" (despite printing a different path!)
// Rename it
def destFile = file('build/Schemas/model.xsd')
destFile.delete()
if (!file('build/Schemas/schema1.xsd').renameTo(destFile)) {
throw new GradleException("Failed to write new build/Schemas/model.xsd")
}
}
}
However, that results in an error:
Error occurred during initialization of boot layer
java.lang.module.ResolutionException: Modules jaxb.runtime and jaxb.core export package com.sun.xml.bind.marshaller to module relaxngDatatype
The issue seems to be known with the jaxb-*:2.3.0 version - #jaxb-v2/issues/1168. Additionally, this would be resolved with a future release as marked in the known issues of jaxb running over java-9.
You can resolve this following the comment -
Please try 2.4.0-b180725.0644 - this is JPMS modularized RI working on
JDKs with java.xml.bind module (9,10) and those without it (11-ea)
or try downloading the binary distribution from the same link.
Schemagen and xjc shell scripts are only put into binary distribution in ./bin directory.
For build tools there are plugins out there (Maven / Gradle) which does invoke schemagen and xjc APIs, providing user with simple configuration.
Your attempt to invoke com.sun.tools.jxc.SchemaGeneratorFacade manually is also correct, here is similar example for Maven. However you are probably putting 2.3.0 on module path, which has a split package problem. Putting on classpath will resolve the issue for 2.3.0. Next release of JAXB will be JPMS ready and have module descriptors declared. You can try the beta build (2.4.0-b180725.0644), here is a correct set of dependencies:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId> <!--jaxb runtime-->
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId> <!--java generation-->
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-jxc</artifactId> <!--schema generation-->
</dependency>
If trying to use the schemagen in the jaxb-ri download directly, note the JAXB_PATH in schemagen.bat is inconsistent with the mod directory in the jaxb-ri.zip file.
E.g. to use schemagen.bat:
As at now, download jaxb-ri-2.3.1.zip from https://maven.java.net/content/repositories/releases/com/sun/xml/bind/jaxb-ri/2.3.1/ (the 2.4.0-betas also had the mod/javax.activation-api.jar missing)
Use these steps to run schemagen.bat, setting JAXB_HOME as appropriate:
set JAXB_HOME=C:\Java\jaxb-ri-2.3.1
set CLASSPATH=%JAXB_HOME%/mod/relaxng-datatype.jar;%JAXB_HOME%/mod/javax.activation-api.jar
%JAXB_HOME%\bin\schemagen.bat -cp myjar1.jar;myjar2.jar -d target com.company.ClassName1 com.company.ClassName2
(This was against JDK 11)
Related
I am new to java and maven. I am learning and trying to make a system to help me build up JSON data by GOOGLE's gson and maven. This is my using command order:
mvn -f my-app\pom xml clean compile
mvn -f my-app\pom xml install
cd my-app\target
java -jar my-app-1.0.jar
After I run it it show me this error.
in GSONExample.java Line 13:
Gson gson = new Gson();
The following is my using software/IDE version.
Source Editor: VS Code
JDK: JDK-19
apache-maven: 3.8.6
Gson: 2.10 (https://github.com/google/gson/releases)
I have already gone through other websites and StackOverflow to find solutions.
GSON is not being imported into the maven project Changed scope role and still crash
error even though it is defined in my classpath /WEB-INF/lib can't find in my situation
Now below a part of my pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
I also had try to import it(Gson) to local and try to fix it out.
mvn install:install-file -Dfile=C:\gson-2.10.jar -DgroupId=com.myself.gson -DartifactId=example-gson -Dversion=2.10 -f my-app\pom.xml
After I use the command, I changed pom.xml as follow. It can compile and install. But still show me "NoClassDefFound"
<dependency>
<groupId>com.myself.gson</groupId>
<artifactId>example-gson</artifactId>
<version>2.10</version>
</dependency>
May anyone provide any solution to this? Thanks in advance.
I had tried
changed scope to provided in pom.xml
put Gson in src\main\resources\lib and change pom.xml
Local gson-2.10.jar and change pom.xml
put gson-2.10.jar in C:\Program Files\apache-maven-3.8.6\lib
The problem is this command:
java -jar my-app-1.0.jar
Most likely your current Maven pom.xml creates a JAR file which contains only the classes of your project, but not the classes of dependencies, such as Gson. This is also not specific to Gson but applies to any dependencies you are trying to use. You can either:
use java -cp and specify the path to your JAR as well as the paths to all the JARs of dependencies you are using, for example java -cp gson-2.10.jar;my-app-1.0.jar com.mycompany.app.GSONExample
configure Maven to build a "JAR with dependencies", see this question whose answers describe multiple ways of how this can be achieved
I'm working on a project where ant + ivy are used for build process.
Since Intellij IDEA is my primary IDE for development I've downloaded IvyIDEA plugin in order to automate the process of resolving dependencies by ivy.
After a while I noticed that not all the dependencies were downloaded, some of them were, but some custom artifacts were missing. I checked the generated list of dependencies by plugin and some of them were actually missing thought the resolving process finished successfully:
report for projName compile produced in C:\Users\test\.ivy2\cache\projName-compile.xml
resolve done (6375ms resolve - 156ms download)
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
post 1.3 ivy file: using exact as default matcher
No problems detected during resolve for module 'ProjName' [All configurations].
I started to dive into ivy logs generated by the plugin and saw this for a bunch of different dependencies:
Sort dependencies of : projToImport;1.1.85300.20210326.5 / Number of dependencies = 8
Non matching revision detected when sorting. projToImportdepends on anotherProj;1.1.81201.20210326.1, doesn't match anotherProj;1.1.81201.20210406.1
Module descriptor is processed : junit#junit;4.11
....
IMPORTANT NOTE: when I do the same in Eclipse IDE with installed Ivy plugin, it all works and can I see all the dependencies described in ivy.xml This makes me think that my ivy-settings.xml and ivy.xml files are correct.
So, my assumption that IvyIDEA plugin doesn't work correct way or something. How is it possible to fix this?
My IveIDEA configuration:
The issue was in the version of ivy: the current version of IvyIDEA plugin (1.0.16) I used contained ivy 2.5.0 and I had to use ivy 2.4.0 in order to run my ivy-settings.xml\ivy.xml.
So the solution for me was to downgrade ivy to version 2.4.0.
I have project used Nashorn Javascript engine. I'm trying to migrate to java11 and also migrate from Nashorn to Graal. I've read here that I can use graal via the standard JDK installation starting from JDK 11. Also I've read there that Graal-SDK are uploaded to Maven central, and that there is Java flag polyglot.js.nashorn-compat for easy migration. So I've used jdk11, add maven dependency to pom.xml and used java flag but when I'm trying to get engine by name "graal.js", I've got null here:
ScriptEngine engine = engineManager.getEngineByName("graal.js")
What I'm missing? How to make it work?
Here is a sample maven project that shows how to run the GraalVM JavaScript engine on JDK11 both through the scripting API and the polyglot API. Hope it helps!
https://github.com/graalvm/graal-js-jdk11-maven-demo
The gist of it is to add the necessary dependencies (graal-sdk, js, js-scriptengine, and optionally profiler and chromeinspector), Run with enabled experimental options and the JVMCI compiler (-XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI) and upgrade the module path with the graal jar (--upgrade-module-path=${compiler.dir}/compiler.jar) which is also available from maven (org.graalvm.compiler:compiler).
You are missing the following dependencies:
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-api</artifactId>
</dependency>
js-scriptengine contains the ScriptEngine implementation: com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.
And the truffle-api is required (you only get the rror message if you instanciate the GraalJSEngineFactory directly:
GraalJSEngineFactory gsf = new GraalJSEngineFactory();
However there seems to be another package missing, as it does not work for me.
From jetbrains blog:
IntelliJ IDEA supports the ability to actually run tests written for
JUnit 5 – there’s no need to use the additional libraries (like the
Gradle or Maven plugins for example), all you need is to include the
JUnit 5 dependency.
I'm new to Java and IntelliJ IDEA and it's not clear to me what are the steps that I should do for making test using Junit 5.
If your project is Maven or Gradle based, the dependency is added via pom.xml or build.gradle, otherwise you just add the .jar files to the Module Dependencies.
IDE can help you with that, press Alt+Enter on the red code:
The following dependencies will be downloaded from the Maven repository and added to the classpath:
I made this work by adding this to my pom:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.0.0-M4</version>
<scope>test</scope>
</dependency>
Previously you need plugin to run unit test like this
buildscript {
repositories {
mavenCentral()
// The following is only necessary if you want to use SNAPSHOT releases.
// maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
But for JUnit5 no need of plugin just compile
dependencies {
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}
Got the latest IntelliJ IDEA (2017.3), being able to add JUnit5 library when creating test class in IntelliJ, but still failed to find tests. Tried the suggestion by #CrazyCoder, and found out the org.junit.jupiter.api has existed in my IntelliJ and has the version 5.0.0-M6. And finally solved by downloading org.junit.platform:junit-platform-commons:1.0.0-M6 from Maven Repository and adding it into classpath.
For someone like me, new to the IntelliJ, the detailed steps I followed:
Open Project Settings -> Libraries -> + New Project Library -> from Maven...
Search and add org.junit.platform:junit-platform-commons:1.0.0-M6
Modules -> module name you want to add it to -> Dependencies -> + 2 Library ... (should have library jar listed)
I add a dependency(let's name it as A) to ivy.xml which has a pom file in maven central. Ivy uses ibiblio for resolving the maven dependencies. The dependency(A) which is added to ivy.xml has a transitive dependency(B). So far so good till here. The dependency(C) of transitive dependency(B) can not be resolved by ivy.
I defined A in ivy.xml like this:
<dependency org="Z" name="A" rev="0.6-SNAPSHOT" conf="*->default"/>
In pom file of B, C is defined both in compile and test scopes like below:
<dependency>
<groupId>X</groupId>
<artifactId>C</artifactId>
</dependency>
<dependency>
<groupId>X</groupId>
<artifactId>C</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
When I look the xml file of B which is resolved by ivy in ivy's cache file(~/.ivy2/cache/X/C/ivy-0.98.8-hadoop2.xml), it looks like this:
<dependency org="X" name="C" rev="0.98.8-hadoop2" force="true" conf="test->runtime(*),master(*)"/>
<dependency org="X" name="C" rev="0.98.8-hadoop2" force="true" conf="test->runtime(*),master(*)">
<artifact name="C" type="test-jar" ext="jar" conf="" m:classifier="tests"/>
</dependency>
For this reason, ivy can not define C scopes correctly. For the record, I don't have permissions to modify the pom files as they are third party projects. How can I fix it ?
I reviewed the ivy usage of the nutch project and apologies but my conclusion is that it's overly complex for the following reasons:
"compile" and "test" targets are issuing separate calls to the resolve task
Each plugin is also calling an ivy resolve task
Complex logic for maintaining classpaths. Could be simplified using the cachepath task and ivy configurations.
Build plugins are not managed by ivy (Sonar, eclipse, rat)
I started to refactor the build, but had to stop when I realised that I didn't understand the relationship between the main nutch artifact and the plugins... (I discovered NUTCH-1515 the hard way... big time-waster The feed plugin has missing dependencies).
I also noticed issue NUTCH-1371 calling for the removal of ivy. This would be a tricky refactoring without significant change to the current codebase. I suspect it would have to be a multi-module build with each plugin listing its own dependencies.
In conclusion, this work does not answer your question, but thought I needed to at least document the result of a few hours analysis :-) In light of NUTCH-1371 I don't know if your project will tolerant major ivy refactoring?
Refactoring ivy
Here follows what I achieved so far:
Private "development" fork of the nutch project
Diff with trunk
Benefits:
Single ivy report showing all configurations (New ivy-resolve target)
New mechanism for installing ivy (New ivy-install target)
Classpaths are managed using ivy configurations (See use of ivy cachepath task and configurations in ivy file)
Eclipse, sonar and rat ANT tasks automatically installed using ivy (The Eclipse plugin is noteworthy as it uses a packager resolver to download and extract jar from a tar archive).
Impacts the following Nutch issues
NUTCH-1881 : This new approach removes resolve-test and resolve-default targets and manages the classpaths using ivy instead of the ${build.lib.dir}
NUTCH-1805 : Can easily setup a separate configuration for the job target with it's own dependencies.
NUTCH-1755 : I think this one is fixed by assigning a name to the the build.xml (see: diff)