قيادة مافن لسرد مراحل دورة الحياة جنبا إلى جنب مع أهداف ملزمة؟

StackOverflow https://stackoverflow.com/questions/1709625

  •  19-09-2019
  •  | 
  •  

سؤال

أنا فقط أتعلم Maven، لذلك قد يكون هذا واضحا، لكن لا يمكنني العثور على طريقة سهلة لإدراج الأهداف المرتبطة بكل مرحلة دورة حياة مافن لمشروع معين.

رأيت أن مراحل دورة الحياة الافتراضية في Maven والأهداف الافتراضية المقابلة هنا. وبعد فهمي حتى الآن هو أن كل pom.xml يمكن أن تربط أهداف إضافية لكل مرحلة دورة حياة.

لذلك، هل هناك أمر MVN لتحديد الأهداف التي سيتم تشغيلها لكل مرحلة دورة حياة لمشروع معين؟ إذا لم يكن الأمر كذلك، أعتقد أنني يجب أن أنظر من خلال 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 سيقوم بطباعة بوم مع جميع المتغيرات وتوسعت جميع بومس الوالدين. هذا يساعد على فهم ما يراه مخيف. من ذلك، من السهل جدا العثور على جميع الأهداف الإضافية (والتي عادة ما لا تكون كثيرا).

المشكلة الأكبر هي الأهداف الضمنية (أي عندما ينفذ البرنامج المساعد نفسه بعض مراحل دورة الحياة تلقائيا). لا توجد طريقة سهلة لرؤية هذه دون تشغيل في الواقع في الواقع. يجب أن يصبح هذا أفضل في 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 على حساب رابطة الأهداف مع المراحل. أحاول أيضا حلها في مستوى مافن.

لقد وضعت إجابة تشاد في برنامج نصي (لذلك ليس لدي أن أتذكر اسم البرنامج المساعد الذي هو طويل حقا). ضعه في مجلد ~ / 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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top