【问题标题】:How resolve conflict with maven如何解决与maven的冲突
【发布时间】:2016-09-13 14:05:00
【问题描述】:

我有 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。

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

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

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

    这些行标识了库的 systemPathscope 让 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        • 2010-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多