简体   繁体   English

Maven运行具有依赖项的项目

[英]Maven run project with dependencies

I found question where run main class but did not found questions where ask how run main class when project have several dependencies. 我发现了在哪里运行主类的问题,但是没有找到问题在项目具有多个依赖项时如何运行主类的问题。

I have follows pom.xml for my project: 我的项目遵循pom.xml

<?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>org.onbrains</groupId>
    <artifactId>ExportQueryResult</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>Runner</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

But when I try run main class from maven: 但是当我尝试从Maven运行主类时:

mvn exec:java

I get follow exception: 我得到以下例外:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.onbrains:ExportQueryResult:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc6:jar should not point at files within the project directory, ${basedir}/lib/ojdbc6.jar will be unresolvable by dependent projects @ line 18, column 25
[WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:exec-maven-plugin is missing. @ line 42, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ExportQueryResult 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ ExportQueryResult ---
oracle.jdbc.OracleDriver
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at query.QueryUtils.executeQuery(QueryUtils.java:41)
    at Runner.main(Runner.java:41)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.008 s
[INFO] Finished at: 2015-12-03T15:15:08+03:00
[INFO] Final Memory: 10M/304M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project ExportQueryResult: An exception occured while executing the Java class. null: InvocationTargetException: NullPointerException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I think oracle jdbc jar is missing in your local maven repository. 我认为本地Maven存储库中缺少oracle jdbc jar。 Download the jar directly from mvn repository as it will add oracle jar to local maven repository. 直接从mvn存储库下载jar,因为它将oracle jar添加到本地maven存储库。

Below is the dependency snippet for oracle jdbc 以下是oracle jdbc的依赖项片段

<dependency>
   <groupId>ojdbc</groupId>
   <artifactId>ojdbc</artifactId>
   <version>14</version>
</dependency>

Change the version as needed by you. 根据需要更改版本。

If you already have an existing jar add to local maven repository. 如果您已经有一个jar,则将其添加到本地Maven存储库。

Anyways, you have to remove <systemPath> tag from your dependency. 无论如何,您必须从依赖项中删除<systemPath>标记。

Try to add classpath : 尝试添加classpath:

 <arguments>
            <argument>-Dmyproperty=myvalue</argument>
            <argument>-classpath</argument>
            <classpath/>
            <argument>Runner</argument>
            ...
 </arguments>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM