Maven -将特定依赖及其传递依赖复制到给予位置

8fsztsew  于 2023-05-06  发布在  Maven
关注(0)|答案(4)|浏览(210)

我有一个maven项目,我把spring framework libraries作为依赖项,我想把spring framework的依赖项复制到指定的位置。
我已经在Apache上浏览了Maven依赖插件指南,我有几个选项,其中没有一个可以解决完整的问题。
1.复制相关性选项

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>

这将复制所有的依赖项和有传递到一个给定的位置,我只想要Spring的依赖项和有传递。
1.复制特定工件

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-web</artifactId>
                   <version>3.2.4.RELEASE</version>
                  <type>jar</type>
                  <overWrite>false</overWrite>                  <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                  <destFileName>optional-new-name.jar</destFileName>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}/wars</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
          </execution>
        </executions>
      </plugin>

这不是处理传递依赖关系。
任何解决我两个问题的方法。

beq87vna

beq87vna1#

这可以通过汇编插件实现。
插件配置:

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/assembly.xml</descriptor>
                </descriptors>
                <finalName>plugins</finalName> <!--folder name in target directory-->
            </configuration>

            <executions>
                <execution>
                    <id>some-id</id> <!-- must match assembly id in assembly.xml-->
                    <phase>pre-integration-test</phase> <!-- pic -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>some-id</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <includes>
                <include>
                    org.springframework:spring-web
                </include>
            </includes>
            <useTransitiveDependencies>true</useTransitiveDependencies>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>

</assembly>

重要的位是<useTransitiveDependencies>true</useTransitiveDependencies><useTransitiveFiltering>true</useTransitiveFiltering>,这导致include应用于项目依赖项,但不应用于传递依赖项,导致spring-web artifact及其依赖项被复制到目录。

6yt4nkrj

6yt4nkrj2#

您可以使用maven汇编插件来实现这一点。
查看它,特别是依赖集:
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet
您可以提供一个输出目录,并可以指定将哪些依赖项放在那里
还有一个选项:useTransitiveDependencies。将其设置为true以获得所需的行为。

kyks70gy

kyks70gy3#

这里有一个选项:

  • 创建模块(生产者)来收集依赖项并将其发布为zip。
  • 在消费者用户依赖性中:unpack打开拉链

这是繁琐的,排除仍然需要一些樱桃采摘,但少得多,它可以在并行线程运行。

制作人

<project 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>packaging</groupId>
  <artifactId>jdbcdrivers</artifactId>
  <packaging>zip</packaging>
  <name>jdbcdrivers</name>
  <dependencies>
<!-- set up deps for capture and limit their impact on anything which depends upon this module via  optional-false -->
<dependency>
    <groupId>net.sf.jtds</groupId>
    <artifactId>jtds</artifactId>
    <scope>test</scope>
    <optional>false</optional>
</dependency>
<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-jdbc</artifactId>
    <scope>test</scope>
    <optional>false</optional>
</dependency>
<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>test</scope>
  <optional>false</optional>
</dependency>

  </dependencies>

  <build>      
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>dist profile: hive jdbc driver ${baseName}</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.outputDirectory}/lib/addons/jdbcdriver/</outputDirectory>
          <useBaseVersion>true</useBaseVersion>
          <excludeTransitive>false</excludeTransitive>
          <overWriteIfNewer>true</overWriteIfNewer>
          <includeScope>test</includeScope>
          <excludeScope>provided</excludeScope>
          <excludeGroupIds>org.codehaus.groovy,org.apache.ivy,jdk.tools</excludeGroupIds>  <!-- you might need to cherry pick excludes if scope doesn't worjk -->
          <prependGroupId>true</prependGroupId>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
  </build>
</project>
t5fffqht

t5fffqht4#

自2.0版起,maven依赖插件在copy-dependencies目标中包含一个名为includeGroupIds的选项。使用该选项(以及其他include-options),您可以仅复制您想要复制的工件,并包含传递依赖项:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.8</version>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
        <includeGroupIds>org.springframework</includeGroupIds>
      </configuration>
    </execution>
  </executions>
</plugin>

相关问题