maven是什么,能够做什么,本文不做介绍,有兴趣的或者不清楚的可自己搜索。
本文介绍将maven作为jar包的形式,通过java代码执行maven命令
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.6.3</version>
</dependency>
一定要引入maven-compat,否则在调用内嵌的maven时,会出现以下错误
1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
while locating org.apache.maven.project.DefaultProjectBuildingHelper
at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)
以下是工具代码,及使用示例
public class MavenDependenciesUtil {
/**复制指定的maven项目依赖的jar到target/lib目录
* @param workingDirectory maven项目目录
* @param localRepository maven本地仓库目录
* @param settings maven settings.xml文件
*/
public static void copy(File workingDirectory, File localRepository,File settings) {
if(!workingDirectory.exists()) {
throw new RuntimeException("指定的maven工程不存在:"+workingDirectory.toString());
}
if(!localRepository.exists()) {
throw new RuntimeException("指定maven本地仓库不存在:"+localRepository.toString());
}
if(!settings.exists()) {
throw new RuntimeException("指定settings.xml文件不存在:"+settings.toString());
}
try {
MavenCli cli = new MavenCli();
//下面这行代码不能缺少,否则无法执行
System.getProperties().setProperty("maven.multiModuleProjectDirectory", "$M2_HOME");
int status = cli.doMain(new String[] {
"-Dmaven.repo.local="+localRepository.getCanonicalPath()
,"-gs="+settings.getCanonicalPath()
,"-DoutputDirectory=./target/lib"
,"dependency:copy-dependencies"
}, workingDirectory.getCanonicalPath(), null, null);
if(status>0) {
throw new RuntimeException("复制依赖的jar到target/lib出错,code="+status);
}
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
}
以下是maven的选项介绍
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-gt,--global-toolchains <arg> Alternate path for the global
toolchains file
-h,--help Display help information
-l,--log-file <arg> Log file where all build output
will go (disables output color)
-llr,--legacy-local-repository Use Maven 2 Legacy Local
Repository behaviour, ie no use of
_remote.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-ntp,--no-transfer-progress Do not display transfer progress
when downloading or uploading
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-t,--toolchains <arg> Alternate path for the user
toolchains file
-T,--threads <arg> Thread count, for instance 2.0C
where C is core multiplied
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-v,--version Display version information
-V,--show-version Display version information
WITHOUT stopping build
-X,--debug Produce execution debug output