2012-09-04 44 views
16

Tôi đã cấu hình các plugin Maven JaCoCo như sau trong tập tin pom.xml của tôi:Maven JaCoCo lỗi Plugin

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <jacoco.version>0.5.9.201207300726</jacoco.version> 
</properties> 

<profiles> 
    <profile> 
     <id>jacoco4</id> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.jacoco</groupId> 
        <artifactId>jacoco-maven-plugin</artifactId> 
        <version>${jacoco.version}</version> 
        <executions> 
         <execution> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration 
          <destfile>${project.build.directory}/target/jacoco.exec</destfile> 
          <datafile>${project.build.directory}/target/jacoco.exec</datafile> 
           <output>file</output> 
           <append>true</append> 
          </configuration> 
         </execution> 
         <execution> 
          <id>report</id> 
          <phase>prepare-package</phase> 
          <goals> 
           <goal>report</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

Tôi đang sử dụng Windows 7 và các plugin apache-maven-3.0.4. Khi tôi gõ mvn -P jacoco4 install, hoặc từ một thiết bị đầu cuối Cygwin hoặc từ một thiết bị đầu cuối dấu nhắc lệnh, Maven tải xuống và chạy plugin JaCoCo, nhưng sau đó tệp jacoco.exec dường như không được tạo ra. Dưới đây là thông báo lỗi:

[ERROR] Unable to read execution data file C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec: C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec (The system cannot find the file specified) 
java.io.FileNotFoundException: C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec (The system cannot find the file specified) 
     at java.io.FileInputStream.open(Native Method) 
     at java.io.FileInputStream.<init>(FileInputStream.java:120) 
     at org.jacoco.maven.ReportMojo.loadExecutionData(ReportMojo.java:251) 
     at org.jacoco.maven.ReportMojo.executeReport(ReportMojo.java:228) 
     at org.jacoco.maven.ReportMojo.execute(ReportMojo.java:217) 
     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) 
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) 
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) 
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) 
     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) 
     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) 
     at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) 
     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) 
     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) 
     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) 
     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) 
     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) 
     at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) 
     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) 
     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) 
     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) 

Thông báo lỗi này xuất hiện hay không tôi bao gồm các destfiledatafile specifiers trong cấu hình của plugin:

<destfile>${project.build.directory}/target/jacoco.exec</destfile> 
<datafile>${project.build.directory}/target/jacoco.exec</datafile> 

Ai đó có thể xin vui lòng cho tôi biết những gì tôi làm sai?

Trả lời

0

tôi sử dụng cấu hình:

<plugin> 
     <groupId>org.jacoco</groupId> 
     <artifactId>jacoco-maven-plugin</artifactId> 
     <version>${jacoco.version}</version> 
      <configuration> 
        <skip>${skipTests}</skip> 
      </configuration> 
      <executions> 
        <execution> 
           <id>jacoco-initialize</id> 
           <phase>initialize</phase> 
           <goals> 
            <goal>prepare-agent</goal> 
           </goals> 
          </execution> 
          <execution> 
           <id>jacoco-site</id> 
           <phase>package</phase> 
           <goals> 
            <goal>report</goal> 
           </goals> 
          </execution> 
      </executions> 
     </plugin> 

Cập nhật: Cấu hình được tạo ra bởi sonar (sonar-pom.xml):

<plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.12</version> 
     <executions> 
      <execution> 
      <id>default-test</id> 
      <phase>test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      <configuration> 
       <excludedGroups>server,ignore,integration</excludedGroups> 
      </configuration> 
      </execution> 
     </executions> 
     <configuration> 
      <excludedGroups>server,ignore,integration</excludedGroups> 
      <argLine>-javaagent:/tmp/jacocoagent3671192291664069011.jar=destfile=target/jacoco.exec,excludes=*_javassist_*</argLine> 
      <testFailureIgnore>true</testFailureIgnore> 
     </configuration> 
     </plugin> 

Một vấn đề - làm thế nào để xác định "jacocoagent3671192291664069011.jar" cho mỗi bản dựng. Nó phải ở:

$M2_HOME/repository/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar 
+0

@kirigiri Bạn đã thử cấu hình của mình chưa? –

+0

Cảm ơn phản hồi của bạn, mà tôi không thấy cho đến ngày hôm nay. Tôi đã thử cấu hình của bạn, ngoại trừ $ {skipTests}, nhưng cấu hình của bạn không giải quyết được sự cố. Bạn có bất cứ ý tưởng khác? – kirigiri

+0

Bạn nói đúng. Tôi chỉ cần sao chép từ dự án của tôi (cha mẹ pom). Kiểm tra được bỏ qua cho một số mô-đun. Tôi sử dụng jenkins với sonar (và jacoco). Ở đó bạn không cần chạy bằng tay. –

21

Tôi gặp rắc rối tương tự với jacoco và maven. Nó có liên quan đến một pom cha mẹ ghi đè lên cấu hình của chắc chắn. Trong trường hợp này, plugin đó không sử dụng đối số (đối số jvm) xác định tác nhân.

Giải pháp là đưa các yếu tố cấu hình "argLine" trở lại

<plugin> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
    <argLine>${argLine}</argLine> 
    </configuration> 
</plugin> 

Full Plugin conf trông giống như

<plugin> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
    <skip>true</skip> 
    </configuration> 
    <executions> 
    <execution> 
     <id>unit-test</id> 
     <phase>test</phase> 
     <goals> 
     <goal>test</goal> 
     </goals> 
     <configuration> 
     <skip>${maven.test.skip}</skip> 
     <argLine>${argLine}</argLine> 
     <excludes> 
      <exclude>**/*IntegrationTest.java</exclude> 
     </excludes> 
     </configuration> 
    </execution> 
    <execution> 
     <id>integration-test</id> 
     <phase>integration-test</phase> 
     <goals> 
     <goal>test</goal> 
     </goals> 
     <configuration> 
     <skip>${skipITs}</skip> 
     <argLine>${argLine}</argLine> 
     <includes> 
      <include>**/*IntegrationTest.java</include> 
     </includes> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
<plugin> 
    <groupId>org.jacoco</groupId> 
    <artifactId>jacoco-maven-plugin</artifactId> 
    <version>0.5.10.201208310627</version> 
    <configuration> 
     <skip>${maven.test.skip}</skip> 
     <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile> 
     <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile> 
     <output>file</output> 
     <append>true</append> 
    </configuration> 
    <executions> 
     <execution> 
      <id>jacoco-initialize</id> 
      <goals> 
       <goal>prepare-agent</goal> 
      </goals> 
     </execution> 
     <execution> 
      <id>jacoco-site</id> 
      <phase>verify</phase> 
      <goals> 
       <goal>report</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

Hy vọng nó sẽ hữu ích

+3

thêm $ {argLine} làm việc cho tôi. Chỉ cần đảm bảo thêm nó vào plugin xây dựng chứ không phải plugin báo cáo (như những gì tôi đã thử trước). Cảm ơn. –

+0

Cảm ơn rất nhiều Làm thế nào để đưa nó vào trang web, hiện tại trang web riêng của nó –

+0

Cảm ơn vì điều này. Tôi đoán người ta phải cẩn thận khi ghi đè cấu hình mặc định của một plugin maven. – dkateros

7

Tôi cũng đã xem qua này vấn đề: JaCoCo không tạo ra tệp đầu ra 'jacoco.exec', có nghĩa là không có phân tích phạm vi mã nào xảy ra.

Trong trường hợp của tôi, đó cũng là do sử dụng một tùy chỉnh argLine trong Maven Surefire Plugin ghi đè JaCoCo Maven Plugin argLine, khiến JaCoCo không được thực thi.

Để sửa lỗi này tôi đã sử dụng các tham số tùy chọn JaCoCo "propertyName" để xuất argLine của mình cho một tài sản Maven và bao gồm rằng trong chắc chắn argLine:

<plugin> 
    <groupId>org.jacoco</groupId> 
    <artifactId>jacoco-maven-plugin</artifactId> 
    <configuration> 
     <propertyName>jacoco.agent.argLine</propertyName> 
    </configuration> 
    ...    
</plugin> 

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.10</version> 
    <configuration> 
     <argLine>-XX:-UseSplitVerifier ${jacoco.agent.argLine}</argLine> 
    </configuration> 
</plugin> 

Tuy nhiên điều này gây ra một vấn đề khi kiểm tra cá nhân đã được chạy trong Netbeans. Vì plugin JaCoCo không được thực thi trong kịch bản này nên biến "jacoco.agent.argLine" không được khởi tạo và chắc chắn không thành công trước khi chạy bất kỳ thử nghiệm nào.

Thêm thuộc tính trống "jacoco.agent.argLine" vào pom đã giải quyết vấn đề khi chạy thử nghiệm đơn lẻ, nhưng điều này cũng đã ngăn JaCoCo xuất khẩu argLine của nó khi nó được thực thi, vô hiệu hóa JaCoCo.

Phần cuối cùng của giải pháp mà tôi đã sử dụng là để thêm một hồ sơ mà tạo ra tài sản trống, và kích hoạt chỉ khi một thử nghiệm duy nhất được quy định:

<profiles> 
    <profile> 
     <activation>     
      <property> 
       <name>test</name>  
      </property> 
     </activation> 
     <properties> 
      <jacoco.agent.argLine></jacoco.agent.argLine> 
     </properties> 
    </profile> 
</profiles> 
+0

Cảm ơn bạn! Đó là bit cuối cùng về hồ sơ kiểm tra là những gì tôi đang tìm kiếm. – nzhenry

13

OK Tôi nghĩ rằng tôi đã tìm ra những gì đang diễn ra.

Theo mặc định, jacoco cắm "chạy" trước khi giai đoạn thử nghiệm (thường là nó chạy mục tiêu prepare-agent trong giai đoạn initialize vòng đời), và khi nó chạy, nó chỉ đặt một tài sản maven gọi là "argLine" một cái gì đó giống như -javaagent=jacoco.jar

ví dụ:

[INFO] argLine set to -javaagent:/usernamed/.m2/repository/org/jacoco/org.jacoco.agent/ 0.5.6.2012/org.jacoco.agent-0.5.6.2012-runtime.jar=destfile=/path/to/target/jacoco.exec

Theo mặc định, maven-surefire-plugin cơ bản "prepends" tài sản này (nếu nó được thiết lập để bất cứ điều gì) để quá trình thử nghiệm java chia hai của nó, vì vậy họ có được hàng hóa. Ex: java ${argLine ends up here}> -jar /xxx/surefirebooter3741906822495182152.jar

thường (không có jacoco), nếu bạn muốn cũng thêm cái gì khác của riêng bạn để argLine đó (ví dụ, -Xmx1G hoặc tương tự), bạn chỉ cần đặt nó trong cấu hình chắc chắn hơn, như

<build> 
    <plugins> 
    <plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <configuration> 
      <argLine>-Xmx1G</argLine> 
      </configuration> 
    </plugin> 

Tuy nhiên, nếu bạn đang sử dụng jacoco, bạn không thể làm điều đó theo cách thông thường bạn làm điều đó bằng cách thiết lập một tài sản toàn cầu, thay vì this way:

<properties> 
    <argLine>-Xmx1G</argLine> 
    </properties> 

Nếu bạn làm thiết lập các <configuration><argLine> sau đó nó về cơ bản ghi đè thuộc tính hệ thống, vì vậy đối số jacoco không được truyền xuống cho tiến trình con. Vì vậy, đó là lý do tại sao bạn sử dụng cách sở hữu thay thế. Nếu bạn chỉ định một thuộc tính argLine thì jacoco sẽ chỉ thêm các tham số của nó vào bất cứ điều gì bạn chỉ định, sau đó chắc chắn sẽ sử dụng nó.

Tuy nhiên, điều gì sẽ xảy ra nếu cha mẹ bạn có đã đặt plugin của <configuration><argLine> thành thứ gì đó? Hoặc nếu bản thân bạn đang thiết lập nó? Về cơ bản nó sẽ sử dụng giá trị đó thay vì thuộc tính mà jacoco đang thiết lập (bạn đã chỉ định ghi đè thủ công).

Nếu bạn đang chỉ định <configuration><argLine>, bạn có thể thay đổi argLine thành thuộc tính (xem bên trên) và xóa <configuration><argLine> và nó sẽ hoạt động. Nếu bạn không thể kiểm soát cha mẹ, và cha mẹ chỉ định một cái gì đó cho argline, sau đó bạn sẽ cần phải đi theo con đường <configuration><argLine>${argLine} -Xmx1G</argLine>. Điều này là để hướng dẫn nó bỏ qua bất cứ điều gì cha mẹ thiết lập giá trị này, và sử dụng argLine thay vào đó (một trong những jacoco đặt cho bạn). (Nó không rõ ràng với tôi nếu có một cách dễ dàng để "thêm" vào giá trị pom cha mẹ có giá trị này, nếu ai biết làm thế nào cảm thấy tự do để bình luận ở đây).

Nhưng nếu jacoco không chạy cho một số mục tiêu hoặc một số cấu hình thì sao? Sau đó biến ${argLine} không bao giờ được thiết lập, và bạn có thể chạy vào một lỗi như thế này:

Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test failed: The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? [ERROR] Command was/bin/sh -c cd ...java '${argLine}' ...

Vâng nó chỉ ra rằng jacoco chỉ "thêm" vào bất động sản tên argLine, khi nó chạy. Vì vậy, bạn có thể thêm một cách an toàn <properties><argLine></argLine></properties> vào pom của bạn (trừ khi bạn đã có sẵn trong pom cha mẹ, sau đó bạn không cần phải thêm bất cứ điều gì). Nếu jacoco từng được gọi, nó sẽ thêm vào nó. Nếu không, nó được đặt thành một chuỗi rỗng, đó là OK. Nó cũng không rõ ràng nếu có một cách để "thêm" vào giá trị của cha mẹ cho một tài sản, do đó, nó hoặc là kế thừa nó, nếu bạn biết nó tồn tại, hoặc chỉ định nó như là sản phẩm nào.

Vì vậy, cuối cùng đối với tôi, kể từ thượng nguồn (không truy cập được) pom mẹ tôi tuyên bố nó như

<configuration><argList>${argList}</argList></configuration>

tôi đã buộc phải về cơ bản tiếp tục con đường đó, vì nó đã được thiết lập đã có trong một (trong kiểm soát của tôi) cha mẹ pom, do đó:

<configuration><argList>${argList} -Xmx1G</argList></configuration>

Các vấn đề liên quan