如何从具有 "Provided" 范围的 maven artifactory 存储库获取特定版本依赖项 jar

How to get specific version dependencies jars from maven artifactory repository who has "Provided" scope

是否有可能从在 build.Due 之后具有 "Provided" 范围的 Maven artifactory 存储库获取特定版本的依赖 jar 到如此多的 webapps 并且其中大多数具有共同的依赖关系,考虑制作所有依赖关系作为 "Provided"(这样 WEB-INF/lib 将是空的)并在部署期间从 artifactory 存储库中获取特定版本的所有 "provided" jar(部署的第一步是将 jar 复制到 Tomcat 公共库,然后是 war 部署)。如果可能,请通过提供模型脚本来帮助我在部署之前从存储库复制到 tomcat 公共库。

假设应用有 3 个 webapp(webapp1、webapp2 和 webapp3)并且都使用 abc1.jar、abc2.jar、abc3.jar.each webapp 类加载器为每个 war 加载所有这 3 个deploy.Instead 按照提供的方式制作它们并在 Tomcat 公共库中保留 3 个罐子是合适的我 feel.Now 我的问题是在 Maven 构建之后,我可以从存储库中获取提供的罐子以从中复制它们吗使用 shell 脚本

到 tomcat 库的存储库

示例pom.xml(未提供范围)

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.group.groupid mSampleJDBCTemprj war 0.0.1-快照 mSampleJDBCTempPrj Maven Web 应用程序 http://maven.apache.org org.springframework spring-上下文 3.1.1.RELEASE 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>

当想法是,创建一个存档,其中包含在 pom.xml 中标有范围 provided 的 jar。因此,如果多个网络应用程序使用这些相同的 jar,它们可以部署到 wen 容器提供程序的中心位置(Tomcat / Jetty / JBoss / 等)。

据我所知 Maven 中没有为这些提供的依赖项创建存档的选项。或者其他一些从 Maven 存储库中轻松提取它们的方法。

一个问题出现你为什么要这样做?现在许多项目转向 Docker 或类似的解决方案。在一个容器中只部署一个网络应用程序。因此无需搜索公共库并将它们预先放置在 Web 容器上的复杂性。等等等等

另一个问题 为什么要增加复杂性。一个更简单的设置是将所有依赖的 jar 添加到网络应用程序。由于磁盘 space 和网络速度/容量,大多数时候都不是问题。

似乎已经提供了答案Getting jars from scope provided maven web project

TIP 上面的例子,展示了maven-dependency-plugin的用法。在 package (<phase>package</phase>).
阶段配置为 运行 使用 mvn clean package,让它完成它的任务。

pom.xml 需要一些小的修改:

  • 这只是一个小 pom.xml,所以 package 应该是 pom,因为没有网络应用内容,Java 类,配置等
  • build中不需要finalName
  • 已更新依赖项 postgresql 它是 scope,提供了值 ,因此插件至少解决了一个依赖项
  • 删除了应该在 parent-pom 情况下使用的 pluginManagement,而不是在这里。这里只是隐藏了插件。在使用它的情况下,parent-pom 定义了多个 Maven 项目的插件配置。所有使用相同父 pom 的项目都可以包含一个插件,具有相同的版本号和配置,如父 pom 中给出的那样。

加法

更正后的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>