简体   繁体   中英

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?

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. 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.

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.

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. I have used it for silencing warnings in automatically generated java files. 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. By now there should be much newer versions of the plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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