如何更改 Maven 2 / Cobertura 工具目标的默认输出?

发布于 2024-08-17 04:29:43 字数 220 浏览 13 评论 0原文

当我使用 Maven 2 使用命令来检测我的类时

mvn cobertura:仪器

输出(检测的类)放在 \target\ generated-classes 中。有没有办法将输出位置更改为\target\classes?

我检查了 cobertura-maven 插件的检测任务,但这到目前为止还没有给我一个解决方案。

when i instrument my classes using Maven 2 using the command

mvn cobertura:instrument

The output (the instrumented classes) are put in \target\generated-classes. Is there a way to change the output location to \target\classes?

I checked the instrumentation tasks of the cobertura-maven plugin but this does not give me a solution sofar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

绿萝 2024-08-24 04:29:43

您还没有说明为什么要覆盖默认位置,但我认为您可以使用其他项目中的检测类,或者将它们包含在 Web 存档或类似的内容中。

我将以下内容添加到我的 pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>instrumented-classes</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>instrumented</classifier>
                <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
            </configuration>
         </execution>
    </executions>
</plugin>

这使得 maven 生成一个名为 projectname-instrumented.jar 的附加 jar 文件

然后可以使用

<depends>
    <group>mygroup</group>
    <project>projectname</project>
    <version>1</version>
    <classifier>instrumented</classifier>
</depends>

我没有测试的 任何其他 pom(包括例如 Web 模块)依赖此 jar 文件这 100% 但过去使用过类似的机制

You have not said why you want to overwrite the default location, but I assume it is so that you can use the instrumented classes from another project, or perhaps include them in a web archive or something similar.

I added the following to my pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>instrumented-classes</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>instrumented</classifier>
                <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
            </configuration>
         </execution>
    </executions>
</plugin>

This makes maven generate an additional jar file called projectname-instrumented.jar

It is then possible to depend on this jar file from any other pom (including for example a web module) using

<depends>
    <group>mygroup</group>
    <project>projectname</project>
    <version>1</version>
    <classifier>instrumented</classifier>
</depends>

I did not test this 100% but have used similar mechanisms in the past

摘星┃星的人 2024-08-24 04:29:43

据我了解,cobertura 仅需要仪器类来生成报告。如果您在target/classes中创建它们,它们将覆盖原始的类文件。

如果您需要 jar 中的检测文件,您可以配置 maven-jar-plugin 来从 target/ generated-classes 目录中获取文件标准 ${build.project.outputDirectory} 中的文件或除此之外的文件。

编辑

看看 maven-jar -插件描述。要仅使用 target/ generated-classes,您的 POM 中的以下添加应该可以工作 - 尝试并根据您的需要进行修改:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version> <!-- replace with correct version nbr! -->
        <configuration>
          <includes>
            <include>${project.build.directory}/generated-classes/**/*.class</include>
          </includes>
          <excludes>
            <exclude>${project.build.directory}/classes/**/*.class</include>
          </excludes>

        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

${project.build.directory} 点到您的目标文件夹,${project.build.ouputDirectory} 到 target/classes。我不知道您是否可以简单地将 ${project.build.ouputDirectory} 设置为新值 - 看看 maven book的这一章,也许你会发现一些提示

编辑2

或者或者另外,您可以在 coberture:instrument 完成后使用 maven 将文件从 target/ generated-classes 复制到 target/classes 。 这个问题有一个答案和一个例子POM(片段),您只需要确定正确的阶段(流程资源对于您的情况来说肯定太早了)

As far as I understand, the instrumented classes are only needed by cobertura for report generation. If you create them in target/classes, they will overwrite the original class files.

If you need the instrumented files in a jar as a result, you can configure the maven-jar-plugin to pick up the files from the target/generated-classes directory instead of or in addition to the files from the standard ${build.project.outputDirectory}.

Edit

Have a look at the maven-jar-plugin description. To only use target/generated-classes, the following addition to your POM should work - try it and modify it to your needs:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version> <!-- replace with correct version nbr! -->
        <configuration>
          <includes>
            <include>${project.build.directory}/generated-classes/**/*.class</include>
          </includes>
          <excludes>
            <exclude>${project.build.directory}/classes/**/*.class</include>
          </excludes>

        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

${project.build.directory} points to your target folder, ${project.build.ouputDirectory} to target/classes. I do not know if you can simply set ${project.build.ouputDirectory} to a new value - have a look at the this chapter of the maven book, maybe you find some hints

Edit 2

Alternativly or additionally you can use maven to copy the files from target/generated-classes to target/classes after coberture:instrument has finished. This question has one answer with an example POM (fragment), you just have to identify the correct phase (process-resources is definitely too early for your case)

旧人哭 2024-08-24 04:29:43

您是否尝试过“mvn cobertura:仪器安装”?它将生成一个包含所有 cobertura 版本类的 jar 文件。
如果您想改回原始版本,只需运行不带“cobertura:instrument”的命令即可。

Did you try "mvn cobertura:instrument install"? It will generate a jar file including all the cobertura version classes.
If you want to change back original version, just run the command without "cobertura:instrument".

沉溺在你眼里的海 2024-08-24 04:29:43

我刚刚实现了 Andreas_D 提出的解决方案,修改了我的 pom 并使用了 maven-resources-plugin。因此,在我构建的某个阶段,Cobertura 生成的文件被复制到 /target/classes 目录。

I just implemented the solution proposed by Andreas_D, modified my pom and uses the maven-resources-plugin. So on some stage of my build the Cobertura generated files are copied to the /target/classes directory.

独﹏钓一江月 2024-08-24 04:29:43

您可以使用 [Your DIR] 进行配置

You can configure it using <classesDirectory>[Your DIR]</classesDirectory>

榕城若虚 2024-08-24 04:29:43

在 cobertura-maven-plugin 版本 2.4 中,这仍然不受支持。我刚刚创建了一个改进票,补丁已附加到票中。

In cobertura-maven-plugin version 2.4 this is still not supported. I've just created an improvement ticket, patch is attached to the ticket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文