将Jar依赖项安装到本地存储库作为Maven构建过程的一部分

时间:2013-11-08 16:28:41

标签: java eclipse maven dependencies

我有一个第三方jar,它是我项目的依赖项。由于业务限制,我无法访问企业或公司存储库,这肯定是我对此问题的偏好。但无论如何,这个第三方jar都不公开,因此它包含在src\main\resources下的Web项目中。

这是一个Maven项目,所以我将这个第三方jar列为编译时依赖项,并在我的pom中包含一个构建插件,它将第三方jar作为构建过程的一部分安装到我的本地存储库;我告诉Maven在构建生命周期的validate阶段执行此目标,据我所知,在任何其他Maven阶段之前。

但是,当我运行clean install并清除我的本地存储库时,由于第三方jar无法在本地或Maven中央存储库中解析,构建失败。据我所知,我已经正确设置了pom,我应该看到Maven尝试在开始依赖解析之前在本地安装第三方jar。

问题是如果在本地安装jar之前列出了依赖项,则构建将因无法解析该依赖项而失败。如果我删除第三方jar声明并运行构建,依赖解析发生后(这是干净后它做的第一件事),但在任何其他阶段之前,它将在本地安装罐子和东西很好。但据我所知,它应该在收集和解析依赖关系之前运行validate阶段,因此jar 应该在Maven解析之前进行本地安装。任何想法或想法?

我的pom:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Web Services</name>
    <description>This project will handle communication.</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!-- This plugin installs the Evip jar from the project's resource folder to the local 
                repository for normal Maven consumption -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>install-evip-jar</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>mvn</executable>
                    <arguments>
                        <argument>install:install-file</argument>
                        <argument>-Dfile=${basedir}\src\main\resources\EVIPSoapServer.jar</argument>
                        <argument>-DgroupId=com.company</argument>
                        <argument>-DartifactId=EVIPSoapServer</argument>
                        <argument>-Dversion=1.0.0</argument>
                        <argument>-Dpackaging=jar</argument>
                    </arguments>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <!-- EXCLUDE EVIPSOAPSERVER JAR FROM CLASSES DIRECTORY -->
                    <packagingExcludes>
                        ${basedir}\src\main\resources\EVIPSoapServer.jar
                    </packagingExcludes>
                    <webResources>
                        <!-- INCLUDE SOURCE FILES WITH WAR -->
                        <resource>
                            <directory>${project.build.sourceDirectory}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <!-- mvn clean install tomcat:run-war to deploy Look for "Running war 
                        on http://xxx" and "Setting the server's publish address to be /yyy" in console 
                        output; WSDL browser address will be concatenation of the two: http://xxx/yyy?wsdl -->
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.0</version>
                    <executions>
                        <execution>
                            <id>start-tomcat</id>
                            <goals>
                                <goal>run-war</goal>
                            </goals>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <port>${test.server.port}</port>
                                <path>/webservice</path>
                                <fork>true</fork>
                                <useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <configuration>
                        <projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
                        <wtpmanifest>true</wtpmanifest>
                        <wtpapplicationxml>true</wtpapplicationxml>
                        <wtpversion>2.0</wtpversion>
                    </configuration>
                </plugin>

                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.codehaus.mojo
                                        </groupId>
                                        <artifactId>
                                            exec-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.2.1,)
                                        </versionRange>
                                        <goals>
                                            <goal>exec</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <!-- COMPILE DEPENDENCIES -->
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>EVIPSoapServer</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.7.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.7.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.7.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <!-- PROVIDED/TEST DEPENDENCIES -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

5 个答案:

答案 0 :(得分:3)

来自http://randomizedsort.blogspot.com.es/2011/10/configuring-maven-to-use-local-library.html的解决方案基于具有基于文件的项目范围的maven存储库。因此,该库在您的源代码版本控制之下。安装是一次性手动过程,而不是pom.xml文件

中指定的内容

三个步骤:

  1. 在项目中创建一个文件夹,您将保留回购。说lib
  2. 使用Maven将jar安装到lib目录。

    mvn install:install-file -Dfile=path_to_mylib.jar ^
        -DgroupId=com.mylib ^
        -DartifactId=mylib ^
        -Dversion=1.0 ^
        -Dpackaging=jar ^
        -DlocalRepositoryPath=path_to_my_project/lib 
    
  3. 3.更新您的pom.xml

        <repositories>
          <repository>
          <!-- DO NOT set id to "local" because it is reserved by Maven -->
            <id>lib</id>
            <url>file://${project.basedir}/lib</url>
          </repository>
        </repositories>
    
        <dependencies>
          <dependency>
            <groupId>com.mylib</groupId>
            <artifactId>mylib</artifactId>
            <version>1.0</version>
          </dependency>
        </dependencies>
    

答案 1 :(得分:2)

在寻找类似问题的解决方案时看到这个问题。我知道它已经过时了,但想分享我的解决方案。我最终直接使用maven-install-plugin来解决我的问题:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>whatevs</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>${oracle.version}</version>
                        <packaging>jar</packaging>
                        <file>${basedir}/dev-setup/lib/ojdbc6.jar</file>
                    </configuration>
                </execution>
            </executions>
        </plugin>

完美无缺。

答案 2 :(得分:1)

我建议你将外部依赖建模为一个单独的项目(将其视为包装器)。您当前的项目可能依赖于您自己的项目,作为其构建的一部分,可以下载外部JAR并将其打包到自己的可分发项中。

答案 3 :(得分:1)

好的,除了争论和偏好之外,我确实选择了桑德的建议;它是唯一一个没有自定义Mojos等真正有用的东西。我有一个类型为pom的包装的父Maven项目,它所做的就是安装第三方jar(可以是任意数量的罐子或者依赖)到我的本地存储库。父母pom:

<?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>com.company</groupId>
    <artifactId>project-evip</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>EvipSoapServerJar</name>
    <packaging>pom</packaging>

    <modules>
        <module>webservices</module>
    </modules>

    <build>
        <plugins>
            <!-- This plugin installs the Evip jar from the project's lib to the local 
                repository for normal Maven consumption -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>install-evip-jar</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>mvn</executable>
                    <arguments>
                        <argument>install:install-file</argument>
                        <argument>-Dfile=${basedir}\src\main\resources\EVIPSoapServer.jar</argument>
                        <argument>-DgroupId=com.company</argument>
                        <argument>-DartifactId=EVIPSoapServer</argument>
                        <argument>-Dversion=1.0.0</argument>
                        <argument>-Dpackaging=jar</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.codehaus.mojo
                                        </groupId>
                                        <artifactId>
                                            exec-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.2.1,)
                                        </versionRange>
                                        <goals>
                                            <goal>exec</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

然后,我在父Maven项目下创建了一个Maven模块并使用了org.apache.cxf.archetype:cxf-jaxws-javafirst:2.7.7原型。我只是将第三方jar列为编译时依赖项,并将其解析并添加到生成的war中。子模块的pom:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>project-evip</artifactId>
        <groupId>com.company</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.company</groupId>
    <artifactId>webservices</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Web Services</name>
    <description>This project will handle communication.</description>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <webResources>
                        <!-- INCLUDE SOURCE FILES WITH WAR -->
                        <resource>
                            <directory>${project.build.sourceDirectory}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <!-- mvn clean install tomcat:run-war to deploy Look for "Running war 
                        on http://xxx" and "Setting the server's publish address to be /yyy" in console 
                        output; WSDL browser address will be concatenation of the two: http://xxx/yyy?wsdl -->
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.0</version>
                    <executions>
                        <execution>
                            <id>start-tomcat</id>
                            <goals>
                                <goal>run-war</goal>
                            </goals>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <port>${test.server.port}</port>
                                <path>/webservice</path>
                                <fork>true</fork>
                                <useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <configuration>
                        <projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
                        <wtpmanifest>true</wtpmanifest>
                        <wtpapplicationxml>true</wtpapplicationxml>
                        <wtpversion>2.0</wtpversion>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <!-- COMPILE DEPENDENCIES -->
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>EVIPSoapServer</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.7.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.7.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.7.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <!-- PROVIDED/TEST DEPENDENCIES -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

感谢所有的帮助,谢谢。

答案 4 :(得分:0)

对应gregm的回答我现在正在使用:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <packaging>jar</packaging>
                        <file>${project.build.directory}/${project.build.finalName}.jar</file>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这会将项目本身的JAR安装到本地存储库。

结果:

--- maven-jar-plugin:2.3.2:jar (default-jar) @ SportyLib ---
Building jar: ...-1.0-SNAPSHOT.jar

--- maven-install-plugin:2.5.2:install (default-install) @ SportyLib ---
Installing ...-1.0-SNAPSHOT.jar to .../.m2/repository/.../1.0-SNAPSHOT/...-1.0-SNAPSHOT.jar
Installing .../pom.xml to .../.m2/repository/.../1.0-SNAPSHOT/...-1.0-SNAPSHOT.pom