使用Maven安装插件安装的本地JAR Maven缺失工件

时间:2018-06-27 16:07:21

标签: java maven jar

我有一个本地jar,需要将其添加为对我的maven项目的依赖,以包含在该项目的已发布jar中。

将其放置在项目中的“ my_project / lib / external1.jar”中,首先,我将其添加到依赖项中,如下所示:

<dependency>
    <groupId>installExternalJars11</groupId>
    <artifactId>externalJar11</artifactId>
    <version>3.1.14</version>
    <systemPath>${project.basedir}//lib/external1.jar</systemPath>
    <scope>system</scope>
</dependency>

它已成功编译,但jar内容未包含在生成的项目jar中,因此,尽管我正在使用以下将打包所有内容的插件,但当我使用项目jar时,这些类的结果为NoClassDefFoundError输出jar中的依赖项

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>

我发现了几篇文章,建议使用maven install插件安装它,然后将其添加为依赖项,因此我遵循以下方法:

<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>com</groupId>
    <artifactId>waheed</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>waheed</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.maven-install-plugin>2.5.2</version.maven-install-plugin>
    </properties>

    <build>
        <plugins>
            <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>${version.maven-install-plugin}</version>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>installExternalJars</groupId>
                    <artifactId>externalJar1</artifactId>
                    <version>3.1.14</version>
                    <file>${project.basedir}/lib/external1.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <executions>
                    <execution>
                        <id>install-ibm-foundation</id>
                        <phase>validates</phase>

                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

    <dependencies>
        <dependency>
            <groupId>installExternalJars11</groupId>
            <artifactId>externalJar11</artifactId>
            <version>3.1.14</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

在Eclipse中,我将依赖项突出显示,并显示以下错误:

Missing artifact installExternalJars11:externalJar11:jar:3.1.14

,当我运行maven build(从eclipse:右键单击pom.xml>作为> maven build运行)时,出现以下错误(显然,maven在我的nexus存储库中查找jar,尽管它在本地被称为本地文件。安装部分):

Failure to find installExternalJars11:externalJar11:jar:3.1.14 
in http://ur_to_nexus/nexus/content/repositories/central/ was cached in 
the local repository, resolution will not be reattempted until the 
update interval of ubknexus has elapsed or updates are forced

尝试了https://stackoverflow.com/a/6112344/458999此处提出的修复程序,但没有用

P.S:当我将Jars添加到eclipse的构建路径(Java构建路径>库>添加Jars)并从eclipse运行项目(或从eclipse导出并运行导出的jar)时,它就起作用了

1 个答案:

答案 0 :(得分:0)

system范围巧妙地使Maven表现出与众不同的特性。

将工件安装在本地Maven存储库服务器(Nexus或Artifactory)中,或在Maven Central上标识等效的工件。如果是商业软件,请要求您的供应商访问其存储库。

如果无法执行此操作,则可以在内部安装作为构建的一部分。在Multiple install:install-file in a single pom.xml中查看如何完成。不过,这应该被视为短期解决方案。