如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?


当前回答

我认为这个问题的更好解决方案是使用maven安装插件在安装时自动安装文件。这是我为项目设置的方式。

首先,将路径(存储local.jar的位置)添加为属性。

<properties>
    <local.sdk>/path/to/jar</local.sdk>
</properties>

然后,在插件下添加一个插件,以便在编译时安装jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>1</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId> 
                <artifactId>appengine-api</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api.jar</file>
            </configuration>
        </execution>
        <execution>
            <id>appengine-api-stubs</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId>
                <artifactId>appengine-api-stubs</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

最后,在依赖项中,可以添加jar

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api-stubs</artifactId>
    <version>1.0</version>
    <scope>test</scope>
</dependency>

通过这样设置项目,即使您将其带到另一台计算机,项目也将继续构建(假定它在local.sdk属性指定的路径中包含所有jar文件)。

对于groupId,请使用唯一的名称,以确保没有冲突。

现在,当您进行mvn安装或mvn测试时,将自动添加本地jar。

其他回答

另一个有趣的例子是,当您希望在项目中拥有私有maven jar时。您可能希望保留Maven的功能来解决可传递的依赖关系。解决方案相当简单。

在项目中创建文件夹库在pom.xml文件中添加以下行<properties(财产)><local.repository.folder>${pom.basedir}/libs/</local.reppository.folder></财产><存储库><存储库><id>本地maven存储库</id><url>文件://${local.resource.folder}</url><发布><enabled>真</enabled></releases><快照><enabled>真</enabled></snapshot></repository></存储库>打开.m2/repository文件夹,将要导入的项目的目录结构复制到libs文件夹中。

例如,假设您要导入依赖项

<dependency>
    <groupId>com.mycompany.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.2.3</version>
</dependency>

只需转到.m2/repository,您将看到以下文件夹

com/mycompany/myproject/1.2.3

复制libs文件夹中的所有内容(同样,包括.m2/repository下的文件夹),就完成了。

根项目上进行构建

您可以在命令行中编写以下代码,或者如果您使用eclipse内置maven,右键单击项目->运行方式->运行配置…->在左侧面板中,右键单击Maven Build->new configuration->在基本目录中的Goals&中编写代码:${project_loc:NameOfYourProject}->运行

mvn install:install-file
   -Dfile=<path-to-file>
   -DgroupId=<group-id>
   -DartifactId=<artifact-id>
   -Dversion=<version>
   -Dpackaging=<packaging>
   -DgeneratePom=true

其中每一个都指:

<path to file>:要加载的文件的路径,例如->c:\kapcha-2.3jar

<group id>:文件应在例如com.google.code下注册的组

<artifact-id>:文件的工件名称,例如->kappcha

<version>:文件的版本,例如->2.3

<package>:文件的打包,例如->jar

2.安装后,只需在pom.xml中声明jar。

 <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
 </dependency>

您可以直接添加本地依赖项(如build maven项目中所述,包含专有库),如下所示:

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>

使现代化

在新版本中,此功能被标记为已弃用,但仍在工作,尚未删除(您只需在maven启动期间的日志中看到警告)。maven集团对此提出了一个问题https://issues.apache.org/jira/browse/MNG-6523(您可以参与并描述此功能在某些情况下有帮助的原因)。我希望这一功能仍然存在!

若你们问我,只要这个特性没有被删除,我就用它来依赖我项目中的一个不适合存储库的jar文件。如果删除此功能,那么,这里有很多好的答案,我可以稍后选择!

在本地存储库中,您可以通过发出以下命令来安装jar

 mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

点击这个有用的链接,从mkyoung的网站进行同样的操作。您也可以查看maven指南

我认为这个问题的更好解决方案是使用maven安装插件在安装时自动安装文件。这是我为项目设置的方式。

首先,将路径(存储local.jar的位置)添加为属性。

<properties>
    <local.sdk>/path/to/jar</local.sdk>
</properties>

然后,在插件下添加一个插件,以便在编译时安装jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>1</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId> 
                <artifactId>appengine-api</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api.jar</file>
            </configuration>
        </execution>
        <execution>
            <id>appengine-api-stubs</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId>
                <artifactId>appengine-api-stubs</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

最后,在依赖项中,可以添加jar

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api-stubs</artifactId>
    <version>1.0</version>
    <scope>test</scope>
</dependency>

通过这样设置项目,即使您将其带到另一台计算机,项目也将继续构建(假定它在local.sdk属性指定的路径中包含所有jar文件)。

对于groupId,请使用唯一的名称,以确保没有冲突。

现在,当您进行mvn安装或mvn测试时,将自动添加本地jar。