我想通过systemPath从本地文件系统相对于我的项目目录结构添加一个jar文件,而不是在远程存储库上。我添加了依赖声明,但maven对它没有做任何其他事情。
在下面的声明中,我希望将jar文件复制到我的目标web-inf / lib目录,并将jar文件作为war文件的一部分。目前,这种情况不会发生。如何将jar文件复制到我的war文件中?
这是调试maven模式的输出:
DEBUG] cglib:cglib-nodep:jar:2.2:test (setting scope to: compile)^M
DEBUG] Retrieving parent-POM: org.objenesis:objenesis-parent:pom:1.2 for project: null:objenesis:ja
DEBUG] org.objenesis:objenesis:jar:1.2:test (selected for test)^M
DEBUG] org.javap.web:testRunWrapper:jar:1.0.0:system (selected for system)^M
DEBUG] Plugin dependencies for:
...
<dependency>
<groupId>org.javap.web</groupId>
<artifactId>testRunWrapper</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/testRunWrapper.jar</systemPath>
</dependency>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
答案 0 :(得分:23)
好的,我这样做了:注意底部的目录结构。 使用下面的方法,来自相对项目路径的jar文件被视为与其他jar一样的一等公民。下面的列表更正了我原来的问题。使用下面的pom.xml列表,jar文件将复制到我的目标目录。
<repositories>
<repository>
<id>JBoss</id>
<name>JBoss Repository</name>
<layout>default</layout>
<url>http://repository.jboss.org/maven2</url>
</repository>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/lib/repo</url>
</repository>
</repositories>
<dependency>
<groupId>testRunWrapper</groupId>
<artifactId>testRunWrapper</artifactId>
<version>1.0.0</version>
</dependency>
$ find repo
repo
repo/testRunWrapper
repo/testRunWrapper/testRunWrapper
repo/testRunWrapper/testRunWrapper/1.0.0
repo/testRunWrapper/testRunWrapper/1.0.0/testRunWrapper-1.0.0.jar
答案 1 :(得分:17)
使用maven依赖插件完成工作:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<includeScope>system</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
答案 2 :(得分:5)
不要使用系统。要执行您想要的操作,只需将其声明为常规(编译)依赖项,并将mvn install:install-file用于本地存储库。其他所有内容都可以按照您的需要运行(lib将被复制等)。这意味着构建只能在您的机器上运行。
要为您的(内部)团队正确解决此问题,您需要设置存储库(例如Artifactory,Nexus或Archiva)。对于团队使用Maven来说,这几乎是必须的。
如果这是用于公共(例如开源)使用,您可以通过http服务器模拟存储库或建立真实的存储库。
答案 3 :(得分:2)
尝试这样的事情(使用Ant插件手动将jar放到输出目录中):
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="${project.basedir}/pathToJAR.jar"
todir="${project.build.directory}/outputFileName/WEB-INF/lib"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
答案 4 :(得分:1)
AFAIK,系统范围的依赖关系有点像提供范围的依赖关系,因此不包含在目标工件中。为什么不将依赖项安装到本地存储库呢?
来自doc:
系统
此范围与提供的类似,只是您必须提供明确包含它的JAR。工件始终可用,不会在存储库中查找。
答案 5 :(得分:0)
使用对文件系统的引用的问题是依赖项目将无法全局访问此jar文件。即依赖项目的$ {basedir}是不同的,因此将找不到.jar文件。
另一方面,全球存储库可以普遍使用。
答案 6 :(得分:0)
如果this answer没有为您工作,因为它对我来说并不知道system
是一个糟糕的范围,您可以尝试使用此解决方案{ {3}}(向下滚动一下),它将JAR安装到您的实际本地Maven存储库中。基本上您只需要将此插件添加到pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>myVersion</version>
<packaging>jar</packaging>
<file>${basedir}/lib/xxx.jar</file>
</configuration>
</execution>
</executions>
</plugin>
填写groupId
,artifactId
和version
的相应值,并将原始jar文件放入<project-home>/lib
目录并修复上面的file
。您可以添加更多execution
个部分,但不要忘记在那里添加id
,例如:
<execution>
<id>common-lib</id>
从代码回购更新的每个人都需要拨打mvn initialize
一次。
所有Eclipse爱好者也可以将此添加到pom.xml
,以摆脱Eclipse中的错误:
<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.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<versionRange>[2.4,)</versionRange>
<goals>
<goal>install-file</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute></execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>