如何让maven 将工程依赖的jar 复制到WEB-INF/lib 目录下并解决 (goals "copy-dependencies", "unpack")

本文介绍如何使用Maven管理项目依赖,包括配置maven-dependency-plugin解决m2e异常问题、执行依赖导出命令、设置依赖级别及清理旧依赖。


注: 用<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包一致。

<plugin>
<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
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.0.RELEASE:repackage (default) on project dataextraction: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.3.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.alihealth.HealthDataProcessor, com.alihealth.TestEntDownLoadList, com.alihealth.TestFileAcceptRet, com.alihealth.Utils.Base64, com.alihealth.Utils.CodeMD5Util, com.alihealth.Utils.KeyStoreUtil, com.alihealth.Utils.StringUtil, com.alihealth.Utils.ZipCom] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.alihealth</groupId> <artifactId>dataextraction</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>dataextraction</name> <url>http://maven.apache.org</url> <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>1.4.3.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.47</version> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>4.0.3</version> <!-- 请检查最新版本 --> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency> <dependency> <groupId>com.taobao.sdk</groupId> <artifactId>taobao-sdk-java</artifactId> <version>20250826</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resource/lib/taobao-sdk-java-auto_1754876302422-20250826.jar</systemPath> </dependency> <!-- <dependency>--> <!-- <groupId>com.taobao</groupId>--> <!-- <artifactId>taobao-sdk</artifactId>--> <!-- <version>20250826</version>--> <!-- </dependency>--> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.3</version> </dependency> <!-- MySQL JDBC --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.32</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.xinghuan</groupId> <artifactId>inceptor-driver</artifactId> <version>8.37.3</version> <scope>system</scope> <systemPath>${basedir}/src/main/resource/lib/inceptor-driver-8.37.3.jar</systemPath> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <exclude>**/*.properties</exclude> </excludes> </configuration> </plugin> <!-- <plugin>--> <!-- <groupId>org.apache.maven.plugins</groupId>--> <!-- <artifactId>maven-jar-plugin</artifactId>--> <!-- <version>3.3.0</version>--> <!-- <configuration>--> <!-- <archive>--> <!-- <manifest>--> <!-- <mainClass>com.alihealth.HealthDataProcessor</mainClass>--> <!-- </manifest>--> <!-- </archive>--> <!-- </configuration>--> <!-- </plugin>--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.0.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRef>jar-with-dependencies</descriptorRef> <archive> <manifest> <mainClass>com.alihealth.HealthDataProcessor</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.alihealth</groupId> <artifactId>dataextraction</artifactId> <version>${project.version}</version> <outputDirectory>${project.build.directory}/dataextraction</outputDirectory> <includes>META-INF/assembly/**</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- <plugin>--> <!-- <artifactId>maven-assembly-plugin</artifactId>--> <!-- <configuration>--> <!--<!– <finalName>${project.name}-spedrug</finalName>–>--> <!-- <descriptor>src/main/assembly/assembly.xml</descriptor>--> <!-- </configuration>--> <!-- <executions>--> <!-- <execution>--> <!-- <id>make-assembly</id>--> <!-- <phase>package</phase>--> <!-- <goals>--> <!-- <goal>single</goal>--> <!-- </goals>--> <!-- </execution>--> <!-- </executions>--> <!-- </plugin>--> </plugins> <resources> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.sh</include> </includes> </resource> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.bat</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> </project> maven打包失败 什么原因
最新发布
10-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值