具有依赖项的 Maven 2 程序集:作用域“system”下的 jar不包括在内

发布于 2024-08-18 14:02:43 字数 1660 浏览 15 评论 0原文

我正在使用 maven-assemble 插件创建我的应用程序的 jar,包括其依赖项,如下所示:(

<assembly>
    <id>macosx</id>
    <formats>
       <format>tar.gz</format>
       <format>dir</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <includes>
                <include>*:jar</include>
            </includes>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

我省略了一些与问题无关的其他内容)

到目前为止,这工作得很好,因为它创建了一个 lib< /code> 包含所有依赖项的目录。但是,我最近添加了一个新的依赖项,其范围是 system,并且它不会将其复制到 lib 输出目录。我一定缺少一些基本的东西,所以我寻求帮助。

我刚刚添加的依赖项是:

<dependency>
  <groupId>sourceforge.jchart2d</groupId>
  <artifactId>jchart2d</artifactId>
  <version>3.1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/external/jchart2d-3.1.0.jar</systemPath>
</dependency>

我能够包含此依赖项的唯一方法是将以下内容添加到 assembly 元素:

<files>
    <file>
        <source>external/jchart2d-3.1.0.jar</source>
        <outputDirectory>lib</outputDirectory>
    </file>
</files>

但是,这迫使我在重命名此 jar 时更改 pom 和程序集文件(如果有的话)。而且,这似乎是错误的。

我尝试过在 dependencySetssourceforge.jchart2d:jchart2d 中使用 runtime > 没有运气。

那么如何将 system 作用域的 jar 包含到 Maven 2 中的程序集文件中呢?

多谢

I am using maven-assembly plugin to create a jar of my application, including its dependencies as follows:

<assembly>
    <id>macosx</id>
    <formats>
       <format>tar.gz</format>
       <format>dir</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <includes>
                <include>*:jar</include>
            </includes>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

(I omitted some other stuff that is not related to the question)

So far this has worked fine because it creates a lib directory with all dependencies. However, I recently added a new dependency whose scope is system, and it does not copy it to the lib output directory. i must be missing something basic here, so I call for help.

The dependency that I just added is:

<dependency>
  <groupId>sourceforge.jchart2d</groupId>
  <artifactId>jchart2d</artifactId>
  <version>3.1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/external/jchart2d-3.1.0.jar</systemPath>
</dependency>

The only way I was able to include this dependency was by adding the following to the assembly element:

<files>
    <file>
        <source>external/jchart2d-3.1.0.jar</source>
        <outputDirectory>lib</outputDirectory>
    </file>
</files>

However, this forces me to change the pom and the assembly file whenever this jar is renamed, if ever. Also, it seems just wrong.

I have tried with <scope>runtime</scope> in the dependencySets and <include>sourceforge.jchart2d:jchart2d</include> with no luck.

So how do you include a system scoped jar to your assembly file in maven 2?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

孤独患者 2024-08-25 14:02:43

我对没有添加系统范围依赖项并不感到惊讶(毕竟,具有系统范围的依赖项必须通过定义显式提供)。实际上,如果您确实不想将该依赖项放入本地存储库中(例如,因为您想将其作为项目的一部分进行分发),那么我会这样做:

  • 我会将依赖项放入“文件系统”中项目本地的存储库”。
  • 我会在我的 pom.xml 中声明该存储库,如下所示:

    <存储库>;
      <存储库>
        我的
        file://${basedir}/my-repo;
      
    
    
  • 我会在没有 system 范围的情况下声明工件,这只是麻烦的根源:

    <依赖>;
      sourceforge.jchart2d;
      jchart2d;
      <版本>3.1.0
    
    

我不能 100% 确定这会满足您的需求,但我认为它是比使用系统范围更好的解决方案。

更新:我应该在原来的答案中提到这一点,我现在正在修复它。要在基于文件的存储库中安装第三方库,请使用 install:install-filelocalRepositoryPath 参数:

mvn install:install-file -Dfile=<path-to-file> \
                         -DgroupId=<myGroup> \
                         -DartifactId=<myArtifactId> \
                         -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> \
                         -DlocalRepositoryPath=<path-to-my-repo>

您可以将其按原样粘贴到 *nix shell 中。在 Windows 上,删除“\”并将所有内容放在一行中。

I'm not surprised that system scope dependencies are not added (after all, dependencies with a system scope must be explicitly provided by definition). Actually, if you really don't want to put that dependency in your local repository (for example because you want to distribute it as part of your project), this is what I would do:

  • I would put the dependency in a "file system repository" local to the project.
  • I would declare that repository in my pom.xml like this:

    <repositories>
      <repository>
        <id>my</id>
        <url>file://${basedir}/my-repo</url>
      </repository>
    </repositories>
    
  • I would just declare the artifact without the system scope, this is just a source of troubles:

    <dependency>
      <groupId>sourceforge.jchart2d</groupId>
      <artifactId>jchart2d</artifactId>
      <version>3.1.0</version>
    </dependency>
    

I'm not 100% sure this will suit your needs but I think it's a better solution than using the system scope.

Update: I should have mentioned that in my original answer and I'm fixing it now. To install a third party library in the file-based repository, use install:install-file with the localRepositoryPath parameter:

mvn install:install-file -Dfile=<path-to-file> \
                         -DgroupId=<myGroup> \
                         -DartifactId=<myArtifactId> \
                         -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> \
                         -DlocalRepositoryPath=<path-to-my-repo>

You can paste this as is in a *nix shell. On windows, remove the "\" and put everything on a single line.

撩心不撩汉 2024-08-25 14:02:43

顺便说一句,您可以将其自动化并使其成为您的 Maven 构建的一部分。以下命令将在编译之前将您的 jar 安装到本地存储库中:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${basedir}/lib/your-lib.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>your-group</groupId>
                        <artifactId>your-artifact</artifactId>
                        <version>0.1</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Btw you can automate it and make it a part of your maven build. The following will install your jar into your local repository before compilation:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${basedir}/lib/your-lib.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>your-group</groupId>
                        <artifactId>your-artifact</artifactId>
                        <version>0.1</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
想念有你 2024-08-25 14:02:43

如果你创建 jar,我找到简单的解决方案

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
    <webResources>
    <resource>
      <directory>dependencies/mydep</directory>
        <targetPath>WEB-INF/lib</targetPath>
        <filtering>true</filtering>
        <includes>
           <include>**/*.jar</include>
        </includes>
    </resource>
  </webResources>
</configuration>
</plugin>

I find easy solution in case you creating jar

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
    <webResources>
    <resource>
      <directory>dependencies/mydep</directory>
        <targetPath>WEB-INF/lib</targetPath>
        <filtering>true</filtering>
        <includes>
           <include>**/*.jar</include>
        </includes>
    </resource>
  </webResources>
</configuration>
</plugin>
感悟人生的甜 2024-08-25 14:02:43

您还可以通过在 dependencySets 中添加补充 dependencySet 来处理此问题。

<dependencySet>
  <scope>system</scope>
  <includes>
    <include>*:jar</include>
  </includes>
  <outputDirectory>lib</outputDirectory>
</dependencySet>

最好的办法是使用存储库管理器(如 Nexus、Artifactory、Archiva)并在特定存储库中安装此类依赖项。之后,您可以使用诸如简单依赖项之类的东西。这将简化您的生活。

文档

You can also handle this via adding a supplemental dependencySet in your dependencySets.

<dependencySet>
  <scope>system</scope>
  <includes>
    <include>*:jar</include>
  </includes>
  <outputDirectory>lib</outputDirectory>
</dependencySet>

The best thing would be to use a Repository Manager (like Nexus, Artifactory, Archiva) and install this kind of dependency in a particular repository. After that you can use such things as a simple dependency. This will simplify your life.

Docs:

岁月静好 2024-08-25 14:02:43

编辑:抱歉,我没有意识到 alx 也提到了干净的生命周期解决方法。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>hack-binary</id>
            <phase>clean</phase>
            <configuration>
                <file>${basedir}/lib/your-lib.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>your-group</groupId>
                <artifactId>your-artifact</artifactId>
                <version>0.1</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

基于alx提供的解决方案,您可以在clean阶段执行安装文件步骤。但由于 clean 阶段不在默认生命周期中,因此您必须在第一时间执行 mvn clean 以确保 jar 在本地存储库中准备就绪。

例如:mvn clean; mvn包

Edited: Sorry that i didn't realize alx also mentioned about the clean life cycle workaround.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>hack-binary</id>
            <phase>clean</phase>
            <configuration>
                <file>${basedir}/lib/your-lib.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>your-group</groupId>
                <artifactId>your-artifact</artifactId>
                <version>0.1</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Base on the solution provided by alx, you can execute the install file step at clean phase. but since the clean phase is not in the default life cycle, you have to execute mvn clean at the first time to ensure the jar is ready in the local repo.

ex: mvn clean; mvn package

千纸鹤带着心事 2024-08-25 14:02:43

一个简单的解决方案是将其添加到本地 Maven 存储库中。

一种方法是通过 mvn install 命令,如上一篇文章中建议的那样。

另一个简单的方法是,
1) 在 Eclipse IDE 中右键单击项目选择 Maven 选项。
2) 选择安装或部署工件到 Maven 存储库选项,然后单击下一步。
3)单击“Artifact file”复选框旁边的“浏览”选择你的jar文件
4)输入GroupId和ArtifactId以及版本确保生成pom&创建校验和并进行检查包装是罐子,

点击完成即可!您的工作已完成,jar 已添加到您的本地存储库中,您可以在setting.xml 或 m2 目录中定义该存储库

现在只需根据 GroupId、ArtifactId & 添加简单的 Maven 依赖项即可。您根据导入输入的 jar 版本,这就是您的外部 jar 将由 maven 打包的。

A simple solution for this is to add it into local maven repository

One way to do is via mvn install commands as suggested in previous post .

Another easy way is ,
1) In your eclipse ide right click on project select Maven option .
2) Select Install or deploy an artifact to a maven repository option and click on next.
3)Click on browse next to the Artifact file checkbox & select your jar file
4)Enter the GroupId and ArtifactId and version ensure generate pom & create checksum are checked & packaging is jar

Click on finish and that's it ! Your job is done the jar is added in your local repository which you can define in setting.xml or m2 directory

Now just add the simple maven dependency as per the GroupId,ArtifactId & jar version that you have entered as per the import and that's it your external jar will be packaged by maven.

独木成林 2024-08-25 14:02:43

它在我的解决方案中以更简单的方式工作:

从依赖项中删除:

<dependency>
  <groupId>tiago.medici</groupId>
  <artifactId>eureka</artifactId>
  <version>0.0.1</version>
</dependency> 

然后也在 pom.xml 中添加 maven-install-plugin 。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <executions>
    <execution>
      <id>install-external</id>
      <phase>clean</phase>
      <configuration>
        <file>${basedir}/external/tiago.medici-0.0.1.jar</file>
        <repositoryLayout>default</repositoryLayout>
        <groupId>tiago.medici</groupId>
        <artifactId>eureka</artifactId>
        <version>0.0.1</version>
        <packaging>jar</packaging>
        <generatePom>true</generatePom>
      </configuration>
      <goals>
        <goal>install-file</goal>
      </goals>
    </execution>
  </executions>
</plugin>

it has worked in a easier way on my solution :

remove from your dependency :

<dependency>
  <groupId>tiago.medici</groupId>
  <artifactId>eureka</artifactId>
  <version>0.0.1</version>
</dependency> 

Then add the maven-install-plugin in the pom.xml as well.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <executions>
    <execution>
      <id>install-external</id>
      <phase>clean</phase>
      <configuration>
        <file>${basedir}/external/tiago.medici-0.0.1.jar</file>
        <repositoryLayout>default</repositoryLayout>
        <groupId>tiago.medici</groupId>
        <artifactId>eureka</artifactId>
        <version>0.0.1</version>
        <packaging>jar</packaging>
        <generatePom>true</generatePom>
      </configuration>
      <goals>
        <goal>install-file</goal>
      </goals>
    </execution>
  </executions>
</plugin>
夢归不見 2024-08-25 14:02:43

简单的解决方案是将 jar 复制到不同的目录

    <build>
    <plugins>
        <!-- Maven Dependency Plugin: Copy system dependencies -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>copy-system-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/extra-lib</outputDirectory> <!-- Copy to a different directory -->
                        <includeScope>system</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>


        <!-- Maven War Plugin: Include extra-lib contents in the WAR -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <webResources>
                    <!-- Include manually copied system scope jars -->
                    <resource>
                        <directory>${project.build.directory}/extra-lib</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

easy solution is to copy jar to a different directory

    <build>
    <plugins>
        <!-- Maven Dependency Plugin: Copy system dependencies -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>copy-system-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/extra-lib</outputDirectory> <!-- Copy to a different directory -->
                        <includeScope>system</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>


        <!-- Maven War Plugin: Include extra-lib contents in the WAR -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <webResources>
                    <!-- Include manually copied system scope jars -->
                    <resource>
                        <directory>${project.build.directory}/extra-lib</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文