如何解决与maven的冲突

时间:2016-09-13 14:05:01

标签: java maven jar

我有pom文件:https://gist.github.com/anonymous/2d2abdc47d868250e8f47d74bdd643c2

我使用命令构建:clean compile assembly:single

但我收到警告:

  

[警告]'dependencies.dependency.systemPath'代表   com.xxx.backtesting:client:jar不应该指向文件内的文件   项目目录,   $ {project.basedir} /lib/client-0.1-jar-with-dependencies.jar将是   依赖项目无法解析@第18行,第25栏

并且这个库在jar文件中不存在:

当我运行我的jar文件时,我得到了:

java -jar backtestingCandlesDownloader-0.1-jar-with-dependencies.jar 1440672480000 1441025280000 60000
task: startDate = 1440672480000, endDate = 1441025280000, period = 60000
Exception in thread "main" java.lang.NoClassDefFoundError: com/xxx/backtesting/client/model/Server
    at com.xxx.backtestingCandlesDownloader.Main.main(Main.java:33)
Caused by: java.lang.ClassNotFoundException: com.xxx.backtesting.client.model.Server
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

我不知道如何在jar文件中包含client-0.1-jar-with-dependencies.jar。

2 个答案:

答案 0 :(得分:2)

您的警告是指您POM的第16行和第18行:

<scope>system</scope>
...
<systemPath>${project.basedir}/lib/client-0.1-jar-with-dependencies.jar</systemPath>

这些行标识了库的systemPath,而scope让Maven知道它是由系统而不是项目[1]提供的。

您最好的方法是将其作为构建的一部分安装在本地Maven资源库中。这将在清理阶段将客户端jar文件作为maven工件安装在本地Maven存储库中,使其可用于依赖项目。作为生命周期的一部分进行安装可确保工件自动可用于所有未来的开发人员/构建。

<build>
    <plugins>
        <!-- Installs new artifact in the local repository -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-artifact</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${project.basedir}/lib/client-0.1-jar-with-dependencies.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.xxx.backtesting</groupId>
                        <artifactId>client</artifactId>
                        <version>0.1</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

安装完成后,您只需将其作为依赖项引用,就像任何其他Maven依赖项一样:

<dependencies>
...
    <dependency>
        <groupId>com.xxx.backtesting</groupId>
        <artifactId>client</artifactId>
        <version>0.1</version>
    </dependency>
...
</depencencies>

或者,您可以通过命令行安装工件,但必须完全限定jar文件的位置[2]。使用命令行方法安装后,您可以像使用Maven中的任何其他依赖项一样使用库:

mvn install:install-file -Dfile=/path/to/lib/client-0.1-jar-with-dependencies.jar -DgroupId=com.xxx.backtesting -DartifactId=client -Dversion=1.0 -Dpackaging=jar
  1. https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
  2. https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

答案 1 :(得分:0)

首先请确保您在项目目录中具有lib / client-0.1-jar-with-dependencies.jar,然后将$ {project.basedir}替换为$ {pom.basedir}

相关问题