2008-10-13 21 views
8

Próbuję skonfigurować kompilację projektu WAR, aby zawiodła, jeśli linia lub zasięg sieci jest poniżej podanych progów. Korzystam z konfiguracji podanej na stronie 455 znakomitej książki Java Power Tools, ale bez powodzenia. Oto odnośny fragment mojego projektu Maven 2 POM:Jak zmusić Coberturę do niepowodzenia kompilacji M2 dla niskiego pokrycia kodu

<build> 
... 
<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-maven-plugin</artifactId> 
    <version>2.2</version> 
    <configuration> 
     <check> 
     <!-- Per-class thresholds --> 
     <lineRate>80</lineRate> 
     <branchRate>80</branchRate> 
     <!-- Project-wide thresholds --> 
     <totalLineRate>90</totalLineRate> 
     <totalBranchRate>90</totalBranchRate> 
     </check> 
     <executions> 
     <execution> 
      <goals> 
      <goal>clean</goal> 
      <goal>check</goal> 
      </goals> 
     </execution> 
     <execution> 
      <id>coverage-tests</id> 
      <!-- The "verify" phase occurs just before "install" --> 
      <phase>verify</phase> 
      <goals> 
      <goal>clean</goal> 
      <goal>check</goal> 
      </goals> 
     </execution> 
     </executions> 
     <instrumentation> 
     <excludes> 
     <exclude>au/**/*Constants.*</exclude> 
     </excludes> 
     <ignores> 
     <ignore>au/**/*Constants.*</ignore> 
     </ignores> 
     </instrumentation> 
    </configuration> 
    </plugin> 
    ... 
</plugins> 
... 
</build> 

Jak mówię, raport pokrycia działa poprawnie, problem jest to, że „instalacja” celem nie zawodzi, jak powinien, jeśli pokrycia wiersz lub oddziale jest poniżej moich określonych progów. Czy ktoś ma to działa, a jeśli tak, to jak wygląda twój POM i którą wersję Cobertury i Mavena używasz? Używam Maven 2.0.9 i Cobertura 2.2.

Próbowałem Googling i czytanie dokumentów Cobertura, ale bez powodzenia (te ostatnie są co najmniej skąpe).

Odpowiedz

15

Według mojej wiedzy, jeśli element <haltOnFailure> jest ustawiony na true, a żadnej z wymienionych kontroli zawiedzie, wtedy Cobertura spowoduje niepowodzenie build, który jest co prosisz. Ale w rzeczywistości element ten domyślnie przyjmuje wartość true, jeśli nie zostanie podana, aby nie trzeba było dodawać go do numeru do swojego . W przypadku braku kompilacji poniżej jakiegokolwiek progu zasięgu jest (lub przynajmniej powinno być) domyślne zachowanie.

EDIT: zrobiłem kilka dalszych badań i haltOnFailure wydaje się działać zgodnie z oczekiwaniami na moim otoczeniu (Maven 2.2.1 i wersje 2.3, 2.2, 2.1 wtyczki tj wersjach 1.9.2, 1.9, 1.8. cobertura w systemie Linux). Aktualizuję tę odpowiedź z wynikiem poniżej.

Właściwie dodałem element <execution> do mojego pom. I może być misinterpreting część dokumentacji cobertura:check „s, która mówi, że«Wiąże domyślnie fazy cyklu życia: verify», ale bez elementu <execution>, cobertura:check nie został wywołany podczas zweryfikować fazie mojej kompilacji. Poniżej konfiguracji mam użyć do Cobertura-maven-plugin:

<project> 
    ... 
    <build> 
    ... 
    <plugins> 
     ... 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>cobertura-maven-plugin</artifactId> 
     <version>2.3</version> 
     <configuration> 
      <check> 
      <!--<haltOnFailure>true</haltOnFailure>--><!-- optional --> 
      <!-- Per-class thresholds --> 
      <lineRate>80</lineRate> 
      <branchRate>80</branchRate> 
      <!-- Project-wide thresholds --> 
      <totalLineRate>90</totalLineRate> 
      <totalBranchRate>90</totalBranchRate> 
      </check> 
     </configuration> 
     <executions> 
      <execution> 
      <phase>verify</phase> 
      <goals> 
       <!--<goal>clean</goal>--><!-- works if uncommented --> 
       <goal>check</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

otrzymuję następujący wynik, gdy uruchomiony mvn clean install na świeżo wygenerowanego projektu maven (z mvn archetype:create) połatany z konfiguracją wtyczki wspomniano powyżej:

$ mvn archetype:create -DgroupId=com.mycompany.samples -DartifactId=cobertura-haltonfailure-testcase 
... 
$ mvn clean install 
[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building cobertura-haltonfailure-testcase 
[INFO] task-segment: [clean, install] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [clean:clean {execution: default-clean}] 
[INFO] Deleting directory /home/pascal/Projects/cobertura-haltonfailure-testcase/target 
[INFO] [resources:resources {execution: default-resources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources 
[INFO] [compiler:compile {execution: default-compile}] 
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/classes 
[INFO] [resources:testResources {execution: default-testResources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources 
[INFO] [compiler:testCompile {execution: default-testCompile}] 
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/test-classes 
[INFO] [surefire:test {execution: default-test}] 
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running com.mycompany.samples.AppTest 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec 

Results : 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

[INFO] [jar:jar {execution: default-jar}] 
[INFO] Building jar: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0-SNAPSHOT.jar 
[INFO] Preparing cobertura:check 
[WARNING] Removing: check from forked lifecycle, to prevent recursive invocation. 
[INFO] [resources:resources {execution: default-resources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources 
[INFO] [compiler:compile {execution: default-compile}] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [cobertura:instrument {execution: default}] 
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file 
Instrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/cobertura 
Cobertura: Saved information on 1 classes. 
Instrument time: 337ms 

[INFO] Instrumentation was successful. 
[INFO] [resources:testResources {execution: default-testResources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources 
[INFO] [compiler:testCompile {execution: default-testCompile}] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [surefire:test {execution: default-test}] 
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running com.mycompany.samples.AppTest 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 sec 

Results : 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

[INFO] [cobertura:check {execution: default}] 
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file 
Cobertura: Loaded information on 1 classes. 

[ERROR] com.mycompany.samples.App failed check. Line coverage rate of 0.0% is below 80.0% 
Project failed check. Total line coverage rate of 0.0% is below 90.0% 

[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] Coverage check failed. See messages above. 
[INFO] ------------------------------------------------------------------------ 
[INFO] For more information, run Maven with the -e switch 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 18 seconds 
[INFO] Finished at: Sat Oct 24 21:00:39 CEST 2009 
[INFO] Final Memory: 17M/70M 
[INFO] ------------------------------------------------------------------------ 
$ 

Nie testowałem z maven 2.0.9, ale na moim komputerze, haltOnFailure generuje BUDUJĄCEGO BŁĄD i zatrzymuje kompilację. Nie widzę żadnych różnic w konfiguracji wtyczki, nie mogę odtworzyć zachowania, które opisujesz.

+0

Powieliłem twój sukces za pomocą twojej konfiguracji, więc teraz muszę tylko zintegrować go z moimi prawdziwymi projektami. BTW, istnieje kilka różnic między moją konfiguracją a twoją, np. mój tag "executions" jest dzieckiem mojego tagu "configuration", podczas gdy twój jest rodzeństwem (jak w książce, nie wiem, dlaczego od niego odbiegam). –

+0

Cieszę się, że wiem.I rzeczywiście, konfiguracja jest inna, co jest naprawdę dobrą rzeczą. Nie wiem, jak to przegapiłem. –

+0

Wiem, że jest to bardzo stara odpowiedź, ale właśnie przypomniałem, że w najnowszej dokumentacji, jest konkretnie poproszony o ustawienie go na "true", jeśli chcesz, aby się nie udało. http://www.mojohaus.org/cobertura-maven-plugin/check-mojo.html –

3

Dodaj następującą konfigurację < check/>.

 
<haltOnFailure>true</haltOnFailure> 
+0

Dziękuję za odpowiedź, ale spróbowałem i to nie zadziałało. Z tego, co przeczytałem o tym ustawieniu, decyduje, czy kompilacja zatrzymuje się po niepowodzeniu testu, czy też kompilacja nie powiedzie się (lub zatrzymuje) z powodu małego zasięgu. Czy to działa dla Ciebie, czy też sugerujesz coś, co możesz spróbować? –