简体   繁体   English

在编译项目时,maven可以更改包声明吗?

[英]Can maven change package declarations when compiling a project?

Well, I have googled this topic many times and I haven't found an answer yet: Can maven change the package declarations right before compiling a project? 好吧,我已经在这个主题上搜索了很多遍,但是还没有找到答案:Maven可以在编译项目之前立即更改包声明吗?

My objective is have project A, which is an API, all in one package, and each project B and C will use the same dependency on eclipse. 我的目标是将项目A(它是一个API)全部放在一个包中,并且每个项目B和C在eclipse上都将使用相同的依赖项。 My objective is that once maven is about to compile the project, the package declarations get changed to that project's specific package and then it compiles the project. 我的目标是,一旦maven将要编译项目,则将包声明更改为该项目的特定包,然后对其进行编译。

This is useful because I have many projects using the same API and I keep making changes to the API itself to suit my needs, but it's a pain to have to go trough every project and change it all again. 这很有用,因为我有许多项目都使用相同的API,并且我会不断修改API本身以适应我的需求,但是不得不仔细检查每个项目并重新进行更改是很痛苦的。

If you have any idea how to do this, and, more importantly, if it is possible, let me know please. 如果您有任何想法,更重要的是,如果有可能,请告诉我。 Thanks in advance! 提前致谢!

Google's maven replacer plugin can do such things, it can edit your java files at build time. Google的maven replacer插件可以做到这一点,它可以在构建时编辑Java文件。 I have used it for silencing warnings in automatically generated java files. 我已使用它在自动生成的Java文件中静默警告。 It can definitely be used for even more evil things. 它绝对可以用于更多邪恶的事情。

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>maven-replacer-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <includes>
                <include>src/main/java/mycodegen/**/*.java</include>
            </includes>
            <regex>true</regex>
            <regexFlags>
                <regexFlag>MULTILINE</regexFlag>
            </regexFlags>
            <replacements>
                <replacement>
                    <token>^public class</token>
                    <value>@SuppressWarnings("all") public class</value>
                </replacement>
            </replacements>
        </configuration>
    </plugin>

This is what I had used. 这就是我用过的。 It should be very similar to change package declarations in Java files. 它与更改Java文件中的程序包声明非常相似。 By now there should be much newer versions of the plugin. 到现在为止,应该有很多新版本的插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM