سؤال

نستخدم MySQL في الإنتاج ، و Derby لاختبارات الوحدة. نسخنا pom.xml نسخ إصدار derby من persistence.xml قبل الاختبارات ، ويحل محله بإصدار mysql في مرحلة الحزم:

 <plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.3</version>
  <executions>
   <execution>
    <id>copy-test-persistence</id>
    <phase>process-test-resources</phase>
    <configuration>
     <tasks>
      <!--replace the "proper" persistence.xml with the "test" version-->
      <copy
       file="${project.build.testOutputDirectory}/META-INF/persistence.xml.test"
       tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
       overwrite="true" verbose="true" failonerror="true" />
     </tasks>
    </configuration>
    <goals>
     <goal>run</goal>
    </goals>
   </execution>
   <execution>
    <id>restore-persistence</id>
    <phase>prepare-package</phase>
    <configuration>
     <tasks>
      <!--restore the "proper" persistence.xml-->
      <copy
       file="${project.build.outputDirectory}/META-INF/persistence.xml.production"
       tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
       overwrite="true" verbose="true" failonerror="true" />
     </tasks>
    </configuration>
    <goals>
     <goal>run</goal>
    </goals>
   </execution>
  </executions>
 </plugin>

المشكلة هي أنه إذا قمت بتنفيذ MVN Jetty: تشغيله ، فسيقوم بتنفيذ مهمة نسخة ملف test Persistence.xml قبل بدء Jetty. أريد تشغيله باستخدام إصدار النشر. كيف يمكنني اصلاح هذا؟

هل كانت مفيدة؟

المحلول

ال jetty:run هدف يستدعي تنفيذ مرحلة دورة الحياة test-compile قبل تنفيذ نفسه. لذلك تخطي الاختبارات إعدام لن يغير أي شيء.

ما عليك القيام به هو ربط copy-test-persistence التنفيذ إلى مرحلة دورة الحياة الخلفية ل test-compile ولكن قبل test. وليس هناك عشرة من المرشحين ولكن واحد فقط: process-test-classes.

هذا من الناحية المفاهيمية ربما ليس مثاليًا ، لكنه الخيار الأقل أسوأ ، وسيعمل:

 <plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.3</version>
  <executions>
   <execution>
    <id>copy-test-persistence</id>
    <phase>process-test-classes</phase>
    <configuration>
     <tasks>
      <!--replace the "proper" persistence.xml with the "test" version-->
      <copy
       file="${project.build.testOutputDirectory}/META-INF/persistence.xml.test"
       tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
       overwrite="true" verbose="true" failonerror="true" />
     </tasks>
    </configuration>
    <goals>
     <goal>run</goal>
    </goals>
   </execution>
   ...
  </executions>
 </plugin>

نصائح أخرى

حاول إضافة الحجج -dmaven.test.skip = true أو -dskiptests = true في سطر الأوامر. فمثلا

mvn -DskipTests=true jetty:run ...

لست متأكدًا مما إذا كان هذا يتخطى مرحلة موارد الاختبار ، رغم ذلك.

مزيد من المعلومات حول تخزين الاختبارات متوفر في وثائق البرنامج المساعد المؤكد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top