简体   繁体   English

如何从具有“已提供”范围的Maven工件存储库中获取特定的版本依赖性jar

[英]How to get specific version dependencies jars from maven artifactory repository who has “Provided” scope

Is it possible to get specific version dependencies jars from maven artifactory repository who has "Provided" scope after the build.Due to so many webapps and most of them having common dependencies, thought of making all dependencies as "Provided" (So that WEB-INF/lib would be empty) and getting all "provided" jars of specific version from the artifactory repository during deployment (Very first step of deployment is copying the jars in to Tomcat common lib followed by war deployment).If possible please help me by giving model script to do the copy from repository to tomcat common lib before deployment. 是否有可能从构建后具有“ Provided”作用域的Maven工件存储库中获取特定的版本依赖项jar。由于有太多的Web应用程序,并且其中大多数都具有公共依赖项,因此考虑将所有依赖项都设置为“ Provided”(这样,WEB- INF / lib将为空),并在部署期间从工件存储库中获取所有“提供的”特定版本的jar(部署的第一步是将jar复制到Tomcat公共lib中,然后进行war部署)。如果可能,请通过提供模型脚本以在部署之前执行从存储库到tomcat common lib的复制。

Assume app having 3 webapp (webapp1,webapp2 and webapp3) and all using abc1.jar,abc2.jar,abc3.jar.each webapp classloader loading all these 3 for each war to deploy.Instead making them as provided and keeping 3 jars in Tomcat common lib would be appropriate i feel.Now my question is after the maven build, can i get provided jars from repository to copy them from repository to tomcat lib using shell script 假设有3个webapp(webapp1,webapp2和webapp3)的应用程序,并且全部使用abc1.jar,abc2.jar,abc3.jar。每个webapp类加载器为每次部署都加载所有这3个应用程序,而不是按提供的方式将它们保存在3个jar中我觉得Tomcat common lib是合适的。现在我的问题是在Maven构建之后,我可以使用存储库脚本从存储库中获取提供的jar并将其从存储库复制到tomcat lib中吗

Sample pom.xml (Without provided scope) 示例pom.xml(不提供范围)

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.group.groupid mSampleJDBCTempPrj war 0.0.1-SNAPSHOT mSampleJDBCTempPrj Maven Webapp http://maven.apache.org org.springframework spring-context 3.1.1.RELEASE cglib cglib 2.2.2 http://maven.apache.org/maven-v4_0_0.xsd“> 4.0.0 com.group.groupid mSampleJDBCTempPrj war 0.0.1-SNAPSHOT mSampleJDBCTempPrj Maven Web应用程序http://maven.apache.org org.springframework spring-context 3.1.1发布cglib cglib 2.2.2

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>
</dependencies>
<build>
    <finalName>mSampleJDBCTempPrj</finalName>
    <pluginManagement>
    <plugins>
        <plugin>
            <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-dependency-plugin</artifactId>
            <version>2.9</version>

            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!-- <includeScope>provided</includeScope> -->
                        <outputDirectory>/Users/venugopal/Documents/providedDependencies</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </pluginManagement>
</build>

When the idea is, to create an archive, which contains the jars, that are marked with the scope provided in the pom.xml . 当想法是,要创建一个包含jar的存档,并用pom.xml 提供的范围进行标记。 So if multiple web-apps use these same jars, they can be deployed to the central place in the wen container provider (Tomcat / Jetty / JBoss / etc). 因此,如果多个Web应用程序使用相同的jar,则可以将它们部署到wen容器提供程序(Tomcat / Jetty / JBoss / etc)中的中央位置。

For as far as I know there is no option in Maven to create an archive, for these provided dependencies. 据我所知 ,对于这些提供的依赖关系,Maven中没有创建存档的选项。 Or some other way to extract them easily from the Maven repository. 或通过其他方式轻松地从Maven存储库中提取它们。

A question rises Why would you do so? 提出了一个问题 ,您为什么要这样做? Many projects move these days to Docker or similar solutions. 如今,许多项目都转移到了Docker或类似的解决方案上。 Which deploy just one web-app in one container. 哪个仅在一个容器中部署一个Web应用程序。 So no need for the complexity of searching for commin libraries and placing them upfront on the web container. 因此,无需搜索commin库并将它们预先放置在Web容器上的复杂性。 Etc etc. 等等

Another question Why add complexity. 另一个问题,为什么要增加复杂性。 An easier set-up is to add all depended jars to the web-app. 较简单的设置是将所有依赖的jar添加到Web应用程序。 As disk space and network speed / capacity, is most of the time not an issue. 由于磁盘空间和网络速度/容量,在大多数情况下不是问题。

Seems the answer Getting jars from scope provided maven web project is already provided 似乎答案已经提供了从提供的Maven Web项目的范围中获取jar

TIP The above sample, shows the usage of the maven-dependency-plugin . 提示上面的示例显示了maven-dependency-plugin的用法。 It is configured to run during the phase package ( <phase>package</phase> ). 它被配置为在阶段程序包<phase>package</phase> )期间运行。
Use mvn clean package , to let it do it's task. 使用mvn clean package ,让它完成任务。

The pom.xml needed a few small modifications: pom.xml需要一些小的修改:

  • This is just a small pom.xml, so package should be pom , as there is no web-app content, Java classes, configuration etc. 这只是一个很小的pom.xml,因此package应为pom ,因为没有Web应用程序内容,Java类,配置等。
  • In build , the finalName is not needed. buildfinalName
  • Updated dependency postgresql it's scope with value provided , so at least one dependency is resolved, by the plug-in 更新依赖postgresqlscope提供的值,所以至少一个依赖性被解析,通过插件
  • Removed pluginManagement which should be used in parent-pom cases, not here. 删除了应该在父pom情况下使用的pluginManagement ,而不是此处。 Here it just hides the plug-in. 在这里它只是隐藏了插件。 In cases where it is used, the parent-pom defines the plugin configuration for multiple Maven projects. 在使用它的情况下,parent-pom定义了多个Maven项目的插件配置。 All projects which use the same parent-pom, can include a plugin, with the same version number and configuration, as given in the parent-pom. 使用相同的父pom的所有项目都可以包含一个插件,该插件具有与父pom中相同的版本号和配置。

Addition 加成

The corrected pom.xml , from the question: 通过以下问题更正后的pom.xml

<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.group.groupid</groupId>
    <artifactId>mSampleJDBCTempPrj</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mSampleJDBCTempPrj Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <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-dependency-plugin</artifactId>
                <version>2.9</version>

                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>provided</includeScope>
                            <outputDirectory>${project.build.directory}/providedDependencies</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM