ライフサイクルフェーズをバインドされた目標とともにリストするMavenコマンド?
質問
私は 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
役立つツールの 1 つが、 mvn help:effective-pom
すべての変数とすべての親 POM が展開された状態で POM が出力されます。これは、Maven が何を認識しているかを理解するのに役立ちます。そこから、追加の目標 (通常はそれほど多くありません) をすべて見つけるのは非常に簡単です。
より大きな問題は暗黙の目標です(つまり、プラグインがライフサイクルのいくつかのフェーズに自動的にフックするとき)。実際に Maven を実行せずにこれらを確認する簡単な方法はありません。これは Maven 3 では改善されるはずです。それまでは、Maven を実行してください -X
これにより、大量のデバッグ出力に加えて、現在のフェーズと実行されるプラグインが出力されます。
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レベルで解決しようとしています。
Chad の回答をスクリプトに入力しました (そのため、非常に長いプラグイン名を覚えておく必要はありません)。~/bin/ フォルダーに配置すると、どこでも使用できます。
#!/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;