I am trying to use Axiom to validate the pom file, but found that except the project can be verified successfully, the rest of the nodes have reported errors
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>library</artifactId>
<build>
<plugins>
<plugin>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
</configuration>
<goals>
<goal>schema</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<groupId>org.apache.avro</groupId>
<version>1.11.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<artifactId>axiom-api</artifactId>
<groupId>org.apache.ws.commons.axiom</groupId>
<version>1.4.0</version>
</dependency>
<dependency>
<artifactId>axiom-impl</artifactId>
<groupId>org.apache.ws.commons.axiom</groupId>
<scope>runtime</scope>
<version>1.4.0</version>
</dependency>
</dependencies>
<groupId>org.test</groupId>
<modelVersion>4.0.0</modelVersion>
<name>library</name>
<properties>
<junit.version>5.8.2</junit.version>
<logback.version>1.2.3</logback.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1.0-SNAPSHOT</version>
</project>
here's my code
#Test
public void commonValidate() throws JaxenException, SAXException, FileNotFoundException {
OMXMLParserWrapper parser = OMXMLBuilderFactory.createOMBuilder(
new FileInputStream("pom.xml"));
AXIOMXPath xpath = new AXIOMXPath("//*");
List<OMElement> elements = xpath.selectNodes(parser.getDocumentElement());
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource schemaSource = new StreamSource(new File("maven-4.0.0.xsd"));
Schema schema = schemaFactory.newSchema(schemaSource);
Validator validator = schema.newValidator();
for (OMElement element : elements) {
try {
validator.validate(element.getSAXSource(true));
System.out.printf("Pom element: %s is valid\n", element.getQName());
} catch (SAXException e) {
//e.printStackTrace();
System.out.println("Pom element is not valid: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading pom element: " + e.getMessage());
}
}
}
the xsd file: https://maven.apache.org/xsd/maven-4.0.0.xsd
This is the log printed on the console
Pom element: {http://maven.apache.org/POM/4.0.0}project is valid
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'modelVersion'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'artifactId'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'build'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'plugins'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'plugin'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'artifactId'.
...
Only the first node is verified, and the rest of the nodes are thrown SAXException.
Related
I am using the blade library in a java 18 project. See code below.
public class Application {
public static void main(String[] args) {
String path = switch (args[0]) {
case "test" -> "/test";
default -> "/default";
};
Blade.create()
.listen(8081)
.get(path, new MyHandler())
.start();
}
private static class MyHandler implements RouteHandler {
#Override
public void handle(RouteContext context) {
context.text("hello");
}
}
}
This code is throwing an exception:
java.lang.IllegalArgumentException: Unsupported class file major version 62
at org.objectweb.asm.ClassReader.(ClassReader.java:176)
at org.objectweb.asm.ClassReader.(ClassReader.java:158)
at org.objectweb.asm.ClassReader.(ClassReader.java:146)
at org.objectweb.asm.ClassReader.(ClassReader.java:284)
at com.hellokaton.blade.asm.ASMUtils.findMethodParmeterNames(ASMUtils.java:30)
at com.hellokaton.blade.mvc.handler.RouteActionArguments.getRouteActionParameters(RouteActionArguments.java:39)
at com.hellokaton.blade.mvc.RouteContext.injectParameters(RouteContext.java:551)
at com.hellokaton.blade.server.RouteMethodHandler.handle(RouteMethodHandler.java:75)
at com.hellokaton.blade.server.HttpServerHandler.executeLogic(HttpServerHandler.java:149)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at java.base/java.lang.Thread.run(Thread.java:833)
This library is written in java 8. I thought that using such switch pattern with enums is incompatible with java 8. However, if I replace one line like in the example below, then it will be executed successfully. Switch-enum pattern hasn't been removed.
// Blade.get() signature:
// public Blade get(String path, RouteHandler handler);
Blade.create()
.listen(8081)
.get(path, new MyHandler()) // <- old (exception)
.start();
Blade.create()
.listen(8081)
.get(path, context -> new MyHandler().handle(context)) // <- new (why works?)
.start();
Using lambda expression instead of object solved the problem. Why didn't the new switch-enum pattern break the code, and what is the reason for the exception then?
Interface RouteHandler looks like this:
#FunctionalInterface
public interface RouteHandler {
void handle(RouteContext var1);
}
My pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github</groupId>
<artifactId>test</artifactId>
<version>1.0-snapshot</version>
<packaging>var</packaging>
<name>test Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>18</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.hellokaton</groupId>
<artifactId>blade-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I want to use Tika as a dependency in a Maven project, to extract metadatas from files. It's working fine when I run the class with mvn exec:java, but not with java -cp, so I suspect it is a dependency problem...
I included all the dependencies in the jar with the maven shade plugin, and at build they are included.
The pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.company.myapp</groupId>
<artifactId>metadata-extractor</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Metadata Extractor</name>
<url>http://maven.apache.org</url>
<properties>
<tika.version>1.19</tika.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Tika -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>${tika.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Main class:
public class App
{
public static void main( String[] args )
{
// Get path
Path path = Paths.get("/path/to/image.jpg");
// Use Tika
TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
Metadata metadata = new Metadata();
AutoDetectParser parser = new AutoDetectParser(tikaConfig);
ContentHandler handler = new BodyContentHandler(-1);
try {
TikaInputStream stream = TikaInputStream.get(path, metadata);
parser.parse(stream, handler, metadata, new ParseContext());
} catch (IOException | SAXException | TikaException e) {
System.out.println("error: " + e.toString());
return;
}
// Prints the metadata and content...
System.out.println("Parsed Metadata: ");
System.out.println(metadata);
System.out.println("Parsed Text: ");
System.out.println(handler.toString());
}
}
Result, with mvn exec:java (working as expected):
Parsed Metadata:
... X-Parsed-By=org.apache.tika.parser.DefaultParser X-Parsed-By=org.apache.tika.parser.jpeg.JpegParser ... other metadatas ...
Parsed Text:
But, with:
mvn clean package
java -cp target/metadata-extractor-1.0-SNAPSHOT.jar org.company.myapp.App
I got:
Parsed Metadata:
X-Parsed-By=org.apache.tika.parser.EmptyParser resourceName=image.jpg Content-Length=1557172 Content-Type=image/jpeg
Parsed Text:
What am I doing wrong? How do I have to build the project for it to correctly autodetect the parser?
Thanks.
There is no parser in your classpath so EmptyParser is chosen. I think the problem is in shade plugin. Remove this line:
<minimizeJar>true</minimizeJar>
And add these dependencies with proper version:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
</dependency>
I created an own maven plugin:
#Mojo(name = "build")
public class InstallerBuilder extends AbstractMojo {
#Parameter(property = "installerBuilderApplication", required = true)
private File installerBuilderApplication;
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("installerBuilderApplication: " + installerBuilderApplication);
if (!installerBuilderApplication.isFile()) {
getLog().error("Please ceheck [installerBuilderApplication] argument is correct.");
throw new MojoExecutionException("Application for installer builder is not a file [" + installerBuilderApplication.getName() + "]");
}
}
}
I am using it in other maven project like:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>company-installer</artifactId>
<groupId>com.company.lsg</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lsg-installer-builder</artifactId>
<name>LSG Installer and Patches Build</name>
<build>
<plugins>
<plugin>
<groupId>com.company.lsg</groupId>
<artifactId>lsg-installer-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<installerBuilderApplication>${basedir}/src/installer/builder_app/NSISPortable/App/NSIS/makensis.exe</installerBuilderApplication>
<installerBase>C:\LSG\Generic_Installer</installerBase>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- inherits config from parent: can override if required -->
</plugin>
</plugins>
</build>
</project>
Where the paramter is marked:
Maven package works, but it somehow bothers me.
Could anyone suggest what I could do to overcome the error!
EDIT: I added the whole pom.xml it is a module of parent. Parent has just three modules declared and pluginmanagement with maven-compile-plugin
I am trying to read pom.xml using dom4j. Document is getting parsed and created successfully however i am not able to read tags.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cycorax</groupId>
<artifactId>test-version</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.cycorax.AspenVersionMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
</project>
My java code:
private static void updateVersion(String pomFile) throws DocumentException {
System.out.println(pomFile);
File inputFile = new File(pomFile);
SAXReader reader = new SAXReader();
Document document = reader.read(inputFile);
System.out.println("Root element : "
+ document.getRootElement().getName());
Document classElement = document.getRootElement().getDocument();
System.out.println(classElement.selectNodes("version"));
System.out.println(classElement.selectSingleNode("version"));
System.out.println(document.selectNodes("project/version"));
System.out.println(document.selectSingleNode("project/version"));
}
Output of code:
H:\Java\test-version\dir\pom.xml
Root element : project
[]
null
[]
Please help why its not able to read other tags.
I have a project that works perfectly if I deploy it outside of a JAR. However, it is unable to get sessions when its deployed as a JAR. Here are the artifacts:
Main.java:
public class Main {
public static void main(String[] args) throws Exception{
JndiContext registry=new JndiContext();
CamelContext context=new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
#Override
public void configure() {
try{
from("jetty:http://0.0.0.0:7700?matchOnUriPrefix=true&sessionSupport=true").bean(HtmlProcessor.class);
}
catch(Exception e){
e.printStackTrace();
}
}
});
context.start();
Thread.sleep(1000000);
context.stop();
}
}
HtmlProcessor.java:
public class HtmlProcessor {
public String login(Exchange exchange){
HttpSession session=exchange.getIn(HttpServletRequest.class).getSession();
Integer count=(Integer)session.getAttribute("count");
if (count==null) count=0;
count++;
session.setAttribute("count", count);
StringBuilder sb=new StringBuilder();
sb.append("<html><body><form>");
sb.append(count);
sb.append("<BR><input type=text name=username></form></body></html>");
return sb.toString();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>user1</groupId>
<artifactId>jettysession</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<camel.version>2.10.3</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifestEntries>
<Bundle-ClassPath>.</Bundle-ClassPath>
<Main-Class>user1.Main</Main-Class>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is the meaningful part of the exception:
java.lang.NullPointerException
at user1.HtmlProcessor.login(HtmlProcessor.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:341)
...
There is a file in META-INF/services/org/apache/camel/ named TypeConverter, which you need to merge with data from all the JARs. When you do this uber JAR I am pretty sure the TypeConverter file is possible just the last file, and therefore you lose data.
See this FAQ at Camel: http://camel.apache.org/how-do-i-use-a-big-uber-jar.html