Cannot find class from jar in maven plugin
I'm making a maven plugin to start, stop and clear a database. I'm using hsqldb for it. I have a class (called ServerStart) to start the database:
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hsqldb.Server;
import org.hsqldb.util.SqlFile;
public static void main(String[] args) {
System.out.println("Starting server...");
createServer();
try {
createADatabase(dbName);
System.out.println("Server started!");
} catch (Exception e) {
e.printStackTrace();
}
}
When I run the main class in Eclipse (right click and run as JavaApplication), it works. But when I try to run it from the cmd line with my mvn command, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hsqldb/Server
at sample.plugin.hello_maven_plugin.ServerStart.createServer(ServerStart
.java:50)
at sample.plugin.hello_maven_plugin.ServerStart.main(ServerStart.java:37)
Caused by: java.lang.ClassNotFoundException: org.hsqldb.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 2 more
When I run the mvn command, he tries to start ServerClass externally, by doing this:
startOptions = new String[] {"java", "-cp", System.getProperty("user.dir") + "/target/classes/", ServerStart.class.getName()};
new ProcessBuilder(startOptions).start();
I guess I forgot something to add on my pom.xml file, so he includes the hsqldb.jar, but I don't have any idea what. This is my pom file:
<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>sample.plugin</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>hsqldb-maven-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</gr开发者_运维百科oupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I hope my question is clear enough, and I also hope that someone can help me.
Kind regards,
Walle
a) ??? You define the plugin:
<groupId>sample.plugin</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
And reference itself as a plugin in the build section?
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>
That's not how it works! A plugin is meant for other projects, not for itself!
b)
startOptions = new String[] {"java", "-cp", System.getProperty("user.dir") + "/target/classes/", ServerStart.class.getName()};
new ProcessBuilder(startOptions).start();
You are going to need a lot more than target/classes in your classpath. The easiest way to get a proper classpath is to let Maven build it for you:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>build-test-classpath</id>
<phase>generate-test-sources</phase>
<goals>
<goal>build-classpath</goal>
</goals>
<configuration>
<outputFile>${project.build.testOutputDirectory}/cp.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
Now you'll find a file named cp.txt on your test classpath which contains the classpath you need. Alternatively, you could just use the contents of System.getProperty("java.class.path")
.
The groupId in your hsqldb dependency is wrong. I had the same problem. Using the following dependency fixed it.
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
</dependency>
精彩评论