在maven插件中无法从jar中找到类

时间:2011-05-16 09:49:38

标签: java jar maven-plugin hsqldb pom.xml

我正在创建一个maven插件来启动,停止和清除数据库。我正在使用hsqldb。我有一个类(称为ServerStart)来启动数据库:

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();
    }
}

当我在Eclipse中运行主类(右键单击并以JavaApplication运行)时,它可以工作。但是当我尝试使用我的mvn命令从cmd行运行它时,我收到此错误:

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

当我运行mvn命令时,他尝试在外部启动ServerClass:

startOptions = new String[] {"java", "-cp", System.getProperty("user.dir") + "/target/classes/", ServerStart.class.getName()};
new ProcessBuilder(startOptions).start();

我想我忘记了在我的pom.xml文件中添加的内容,所以他包含了hsqldb.jar,但我不知道是什么。这是我的pom文件:

<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</groupId>
    <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>

我希望我的问题很清楚,我也希望有人可以帮助我。

亲切的问候,

WALLE

2 个答案:

答案 0 :(得分:1)

a)???您定义插件:

<groupId>sample.plugin</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

并在构建部分引用自己作为插件?

<plugin>
    <groupId>sample.plugin</groupId>
    <artifactId>hsqldb-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>         
</plugin>

这不是它的工作方式!插件适用于其他项目,而非其自身!

b)中

startOptions = new String[] {"java", "-cp", System.getProperty("user.dir") + "/target/classes/", ServerStart.class.getName()};
new ProcessBuilder(startOptions).start();

在类路径中,您需要的不仅仅是目标/类。获得正确类路径的最简单方法是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>

现在,您将在测试类路径中找到一个名为cp.txt的文件,其中包含您需要的类路径。或者,您可以使用System.getProperty("java.class.path")

的内容

答案 1 :(得分:0)

hsqldb依赖项中的groupId是错误的。我有同样的问题。使用以下依赖项修复它。

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.0.0</version>
    </dependency>