在对项目进行打包的时候,可能会遇见这样的需求,那就是将当前项目依赖的jar包,复制到某个路径,例如lib
目录,在maven体系中,需要使用对应的插件,而在gradle
则只需要编写对应的脚本即可。
在maven中的方式 在maven中,只需要使用对应的build插件即可,插件代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-dependency-plugin</artifactId > <version > 3.1.1</version > <executions > <execution > <id > copy-dependencies</id > <phase > package</phase > <goals > <goal > copy-dependencies</goal > </goals > <configuration > <outputDirectory > ${project.basedir}/lib</outputDirectory > <includeScope > runtime</includeScope > </configuration > </execution > </executions > </plugin > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-antrun-plugin</artifactId > <version > 1.8</version > <executions > <execution > <id > clean</id > <phase > prepare-package</phase > <configuration > <target > <delete quiet ="true" > <fileset dir ="${project.basedir}/lib" includes ="*.*" /> </delete > </target > </configuration > <goals > <goal > run</goal > </goals > </execution > <execution > <id > copy</id > <phase > package</phase > <configuration > <target > <copy file ="${project.build.directory}/${project.artifactId}-${project.version}.jar" tofile ="${project.basedir}/lib/${project.artifactId}-${project.version}.jar" /> </target > </configuration > <goals > <goal > run</goal > </goals > </execution > </executions > </plugin >
两个插件的作用分别将项目依赖的包以及自己本身的jar包拷贝到lib目录中。
在gradle
中的方式 在gradle
中,我们只需要编写对应的任务,然后将其放在build任务执行之后执行即可,首先看下具体脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 configurations { customConfig.extendsFrom api } task copyLib(type: Copy) { def libPath = projectDir.absolutePath + '/lib' println "${project.name} do copyLib : ${libPath}" def libDir = file(libPath) if (libDir.exists()) { libDir.listFiles().findAll { it.name.endsWith('.jar' ) }.collect { delete it } } dependsOn jar String thisJarName = "${archivesBaseName}-${version}.jar" String thisJarPath = "${getBuildDir()}/libs/${thisJarName}" from(file(thisJarPath)) into file(libPath) from(project.configurations.customConfig) into(libPath) } build.finalizedBy(copyLib)
注意:
这里我使用了api
去设置依赖,使用api
需要在build.gradle
中引入插件:apply plugin: 'java-library'
在configurations
中定义了customConfig
,这是由于如果在from
中直接使用configurations.api
,则会报如下错误。
1 2 Resolving dependency configuration 'api' is not allowed as it is defined as 'canBeResolved=false'. Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'api' should be resolved.
同时也可能会报这样的错误
1 Could not get unknown property 'api' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
参考网络上的,说推荐使用configurations.implementation
或者configurations.compile
,但可能由于gradle
版本(这里我使用的是gradle 7.0
)的不同,以及项目构建的不同,都会报Could not get unknown property
的错误。推荐还是在configurations
中定义一个属性,这里我是定义了一个customConfig
并且他继承了api
,所以能够将依赖拷贝到lib目录下