注: 用<pluginManagement> 将<plugins>包裹好,可以解决 maven-dependency-plugin (goals copy-dependencies unpack ) is not supported by m2e 异常问题
<build>
<!-- Ignore/Execute plugin execution -->
<pluginManagement>
<plugins>
<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-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
1.在默认生命周期的compile阶段执行 maven-dependency-plugin:copy-dependencies命令即可:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.当依赖jar包发生变更时,应该执行clean生命周期,删除所有旧的jar包;这样才能保证WEB-INF/lib和Maven的依赖管理的Jar包一致。
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<execution>
<id>clean</id>
<phase>pre-clean</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<directory>src/main/webapp/WEB-INF/lib</directory>
</configuration>
</execution>
</executions>
</plugin>
项目路径:
${project.build.directory}
另一种方法:
一、导出到默认目录 targed/dependency
从Maven项目中导出项目依赖的jar包:进入工程pom.xml 所在的目录下,执行如下命令:
1
|
mvn dependency:copy-dependencies
|
或在eclipse中,选择项目的pom.xml文件,点击右键菜单中的Run As,见下图红框中,在弹出的Configuration窗口中,输入 dependency:copy-dependencies后,点击运行
maven项目所依赖的jar包会导出到targed/dependency目录中。
二、导出到自定义目录中
在maven项目下创建lib文件夹,输入以下命令:
1
|
mvn dependency:copy-dependencies -DoutputDirectory=lib
|
maven项目所依赖的jar包都会复制到项目目录下的lib目录下
三、设置依赖级别
同时可以设置依赖级别,通常使用compile级别
1
|
mvn dependency:copy-dependencies -DoutputDirectory=lib -DincludeScope=compile
|