2011-05-16 8 views
1

데이터베이스를 시작, 중지 및 지우는 데 maven 플러그인을 만들고 있습니다. 나는 그것을 위해 hsqldb를 사용하고있다. I (오른쪽 버튼으로 클릭하고 JavaApplication로 실행) 이클립스의 주요 클래스를 실행하면maven 플러그인에서 jar 파일의 클래스를 찾을 수 없습니다.

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

이 작동 : I 클래스 (라고 ServerStart)는 데이터베이스를 시작해야합니다.

startOptions = new String[] {"java", "-cp", System.getProperty("user.dir") + "/target/classes/", ServerStart.class.getName()}; 
new ProcessBuilder(startOptions).start(); 
:이 작업을 수행하여, 나는 MVN 명령을 실행했을 때, 그는 외부하여 ServerClass을 시작하려고

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 명령을 cmd를 줄에서 실행하려고 할 때, 나는이 오류

내 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

답변

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")의 내용을 사용할 수도 있습니다.

+0

플러그인에서 build-test-classpath를 찾을 수 없습니다. – Walle

+0

@Walle, 미안, 이드와 목표를 혼동스럽게 생각합니다. fixed now –

+0

그 텍스트 문서에 많은 .jar 파일들이 있고 hsqldb.jar도 없습니다. 'System.getProperty ("java.class.path")'를 사용하면 텍스트 파일에 인쇄되는 모든 .jar 파일이 포함됩니까? 그걸로 이미 시도했기 때문에 작동하지 않았습니다. 어쩌면 내가 뭔가 잘못했을 수도 있습니다. – Walle

0

hsqldb 종속성의 groupId가 잘못되었습니다. 나는 똑같은 문제가 있었다. 다음 종속성을 사용하여 해결했습니다.

<dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>2.0.0</version> 
    </dependency> 
관련 문제