题
我刚刚学习 Maven,所以这可能是显而易见的,但我找不到一种简单的方法来列出给定项目的每个 Maven 生命周期阶段相关的目标。
我看到 Maven 默认生命周期阶段和相应的默认目标都有记录 这里. 。到目前为止,我的理解是每个 pom.xml 都可以将附加目标绑定到每个生命周期阶段。
那么,是否有一个 mvn 命令来确定给定项目的每个生命周期阶段将运行的目标?如果没有,我想我只需要查看每个新的 Maven 项目的 pom.xml 即可解决这个问题?
解决方案
mvn help:describe -Dcmd=compile
(或任何其他有效的相)
其他提示
在 buildplan-maven-plugin
是一个极好的工具用于显示目标是如何结合到相。
下面是可以通过命令的例子。该命令会自动下载,如果尚未安装它安装插件。
通过的顺序列表的目标,他们将执行
> mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list
PLUGIN | PHASE | ID | GOAL
--------------------------------------------------------------------------------------------
maven-enforcer-plugin | validate | default | enforce
maven-dependency-plugin | process-sources | default | copy-dependencies
maven-resources-plugin | process-resources | default-resources | resources
maven-compiler-plugin | compile | default-compile | compile
maven-resources-plugin | process-test-resources | default-testResources | testResources
maven-compiler-plugin | test-compile | default-testCompile | testCompile
maven-surefire-plugin | test | default-test | test
maven-jar-plugin | package | default-jar | jar
maven-assembly-plugin | package | make-assembly | single
maven-install-plugin | install | default-install | install
maven-deploy-plugin | deploy | default-deploy | deploy
组的目标通过相
> mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list-phase
validate -----------------------------------------------------------------
+ maven-enforcer-plugin | default | enforce
process-sources ----------------------------------------------------------
+ maven-dependency-plugin | default | copy-dependencies
process-resources --------------------------------------------------------
+ maven-resources-plugin | default-resources | resources
compile ------------------------------------------------------------------
+ maven-compiler-plugin | default-compile | compile
process-test-resources ---------------------------------------------------
+ maven-resources-plugin | default-testResources | testResources
test-compile -------------------------------------------------------------
+ maven-compiler-plugin | default-testCompile | testCompile
test ---------------------------------------------------------------------
+ maven-surefire-plugin | default-test | test
package ------------------------------------------------------------------
+ maven-jar-plugin | default-jar | jar
+ maven-assembly-plugin | make-assembly | single
install ------------------------------------------------------------------
+ maven-install-plugin | default-install | install
deploy -------------------------------------------------------------------
+ maven-deploy-plugin | default-deploy | deploy
组的目标通过插件
> mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list-plugin
maven-enforcer-plugin ---------------------------------------------------
+ validate | default | enforce
maven-dependency-plugin -------------------------------------------------
+ process-sources | default | copy-dependencies
maven-resources-plugin --------------------------------------------------
+ process-resources | default-resources | resources
+ process-test-resources | default-testResources | testResources
maven-compiler-plugin ---------------------------------------------------
+ compile | default-compile | compile
+ test-compile | default-testCompile | testCompile
maven-surefire-plugin ---------------------------------------------------
+ test | default-test | test
maven-jar-plugin --------------------------------------------------------
+ package | default-jar | jar
maven-assembly-plugin ---------------------------------------------------
+ package | make-assembly | single
maven-install-plugin ----------------------------------------------------
+ install | default-install | install
maven-deploy-plugin -----------------------------------------------------
+ deploy | default-deploy | deploy
备注
默认情况下,目标搜索,如果用户调用mvn deploy
,将运行任务。相如clean
将不被包括在内。到包括在所述搜索多个阶段,使用buildplan.tasks
属性:
> mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list -Dbuildplan.tasks=clean,deploy
一个工具,可以帮助是mvn help:effective-pom
这将打印扩大所有变量和所有家长的POM聚甲醛。这有助于了解Maven的看到的东西。从这一点,这是很简单的找到所有的其他目标(通常是没有那么多)。
在更大的问题是隐式目标(即,当自动插件钩本身到生命周期的某些阶段)。有没有简单的方法来看到这些不实际运行的Maven。这应成为在Maven的3更好的在此之前,与-X
将打印一大堆调试输出加上当前阶段和插件执行运行Maven。
如果不使用 Maven,而是使用 m2e,您可以使用可在 Eclipse 插件中使用的代码块来完成此操作:
final IMavenProjectRegistry projectRegistry = MavenPlugin.getMavenProjectRegistry();
final IMavenProjectFacade facade = projectRegistry.getProject(project);
projectRegistry.execute(facade, new ICallable<Void>() {
public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
MavenProject mavenProject = facade.getMavenProject(monitor);
List<MojoExecution> mojoExecutions = ((MavenProjectFacade) facade).getMojoExecutions(monitor);
LifecycleMappingResult mappingResult = LifecycleMappingFactory.calculateLifecycleMapping(
mavenProject, mojoExecutions, facade.getResolverConfiguration().getLifecycleMappingId(),
monitor);
Map<MojoExecutionKey, List<IPluginExecutionMetadata>> mojoExecutionMapping = mappingResult
.getMojoExecutionMapping();
Map<String, List<MojoExecutionKey>> phases = new LinkedHashMap<String, List<MojoExecutionKey>>();
for (MojoExecutionKey execution : mojoExecutionMapping.keySet()) {
List<MojoExecutionKey> executions = phases.get(execution.getLifecyclePhase());
if (executions == null) {
executions = new ArrayList<MojoExecutionKey>();
phases.put(execution.getLifecyclePhase(), executions);
}
executions.add(execution);
}
看看完整的 来源.
已经实施于:
http://marketplace.eclipse.org/content/phases-and-goals
它利用 m2e 的能力来计算目标与阶段的关联。我也在尝试在 Maven 级别解决它。
我把乍得的回答到一个脚本(所以我没有记住这是很长的插件名称)。把它放在你的〜/斌/文件夹,这样你可以在任何地方使用它。
#!/usr/bin/env bash
# Created based on https://stackoverflow.com/a/35610377/529256
debug=false
goal='list-phase'
build_plan='clean,deploy'
working_directories=""
for (( i=1; i<=$#; i++ )) do
case ${!i} in
-h|--help)
programName=$( basename ${0} )
echo "Lists the goals of mvn project(s) by phase in a table";
echo
echo "Usage:";
echo " ${programName} -d|--debug -g|--goal goal -b|--build_plan build_plan [*directory]";
echo
echo " --goal The goal for the buildplan-maven-plugin (default: $goal)"
echo " (possible values: list, list-plugin, list-phase)"
echo
echo " --build_plan The value of the buildplan.tasks parameter (default: $build_plan)"
echo " (examples: 'clean,install', 'deploy', 'install', etc...) "
echo
echo " [*directory] The directories (with pom.xml files) to run the command in"
exit 0;
;;
-d|--debug)
debug=true;
echo "debug = ${debug}";
;;
-b|--build_plan)
((i++))
build_plan="${!i}"
;;
-g|--goal)
((i++))
goal="${!i}"
;;
*)
working_directory="${!i}";
if [ ! -e "${working_directory}" ]; then
echo "'${working_directory}' doesn't exist";
exit 1;
fi;
if [ -z "${working_directories}" ]; then
working_directories="$working_directory"
else
working_directories="$working_directories ${!i}"
fi;
;;
esac;
done;
if [ -z "${working_directories}" ]; then
working_directories="$PWD"
fi
if [ ${debug} = true ]; then
echo "working_directories=$working_directories"
echo "goal=$goal"
echo "build_plan=$build_plan"
fi
for workingDirectory in ${working_directories}; do
pushd ${workingDirectory} > /dev/null
echo "cd $workingDirectory"
echo "mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:${goal} -Dbuildplan.tasks=${build_plan}"
mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:${goal} -Dbuildplan.tasks=${build_plan}
popd > /dev/null
done;