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

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

问题描述

我已经有一个工作解决方案,我可以在其中使用 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

我目前的解决方案就像

<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 

在课堂上会很不错.

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

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 模块中,那么代码应该总是被编译.但是,您可以使用 maven-jar-pluginpackage 阶段创建多个工件:默认的 artifactId.jar 和一个 artifactId-test-lib.jar 工件.您可以通过为插件指定多次执行来实现此目的,并使用 根据需要拆分 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.

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

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