6

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

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

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

问题是,如果在本地安装 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>
4

5 回答 5

5

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

三个步骤:

  1. 在您的项目中创建一个文件夹,您将在其中保存 repo。说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.更新你的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>
于 2015-05-12T20:46:38.867 回答
3

在寻找类似问题的解决方案时看到了这个问题。我知道它很旧,但想分享我的解决方案。我最终通过直接使用 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>

完美运行。

于 2014-10-09T13:52:21.260 回答
2

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

于 2013-11-08T16:52:42.410 回答
1

好吧,除了争论和偏好,我确实接受了 Sander 的建议。它是唯一一个在没有自定义 Mojos 等的情况下真正工作的项目。我有一个带有类型包装的父 Maven 项目,pom它所做的只是将第三方 jar(可以是任意数量的 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 列为编译时依赖项,然后将其解析并添加到生成的战争中。子模块的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>

我感谢所有的帮助,谢谢。

于 2013-11-08T19:54:24.700 回答
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
于 2014-10-11T12:09:23.183 回答