如何从注释中排除Java类在Maven中的编译 [英] How to exclude java classes from being compiled in maven with annotation

查看:49
本文介绍了如何从注释中排除Java类在Maven中的编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个有效解决方案,在该解决方案中,我可以使用maven指定在使用特定的maven配置文件时不编译哪些类.

I already have a working solution where I can specify with maven which classes to not compile when using a particular maven profile.

但是我想使用一般的解决方案,而是使用注释

But I would like to use a general solution and use an annotation instead

我当前拥有的解决方案就像

The current solution that I have is like

<plugin>
    <!-- Exclude some web services used only for internal testing -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <optimize>true</optimize>
        <excludes>
            <exclude>**/something/*ClassPattern.java</exclude>
        </excludes>
        <testExcludes>
            <exclude>**/something/*ClassPatternTest.java</exclude>
        </testExcludes>
    </configuration>
</plugin>

但是类似

@NotCompiledForProduction 

在课堂上会很好.

在我看来,如果不改变行家的行为,这可能很难(或不可能做到).这不是这里的范围.还有这种注释

It seems to me that this might be hard (or impossible to do) without changing maven's behaviour. That is not the scope here. And this kind of annotation

推荐答案

您不能(我认为)使用批注来确定将哪些源代码呈现给Java编译器,因为您首先需要编译源代码处理注释.

You cannot (I assume) use an annotation to determine what source code gets presented to the java compiler, because you need to compile the source code in the first place to process the annotation.

似乎您需要在maven项目中创建不同的模块:一个模块使用生产代码生成jar文件,而一个模块通过测试实现依赖生产工件来生成jar文件.

It seems like you need to create different modules in your maven project: one that generates a jar file with the production code, and one module that generates a jar file with testing implementation with a dependency on the production artifact.

如果代码确实确实需要在同一Maven模块中,则应始终编译代码.但是,您可以在 package 阶段使用 maven-jar-plugin 创建多个工件:默认的 artifactId .jar ,以及 artifactId -test-lib.jar 工件.您可以通过为插件指定多次执行,并使用< includes> < excludes> 来根据需要拆分jar文件来执行此操作.

If the code really does need to be in the same maven module, then the code should always be compiled. You can however use maven-jar-plugin to create multiple artifacts at the package phase: the default artifactId.jar, and an artifactId-test-lib.jar artifact. You can do this by specifying multiple executions for the plugin, and using <includes> and <excludes> to split the jar files as required.

这篇关于如何从注释中排除Java类在Maven中的编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆