2

I am looking for a way to add the annotation to field during build time. Is it possible to modify the '.class' file content during build time? For my case,

Class A{
x;
y;
}

I have different customer code base. Some customer needs only x field to be annotated, some needs only y to be annotated. So I am looking for adding the annotation during build time.

  • Why do you want to do this? –  Jul 22 '19 at 09:59
  • You can probably achieve the same effect much easier with some external configuration. What annotation are we talking about? – Thilo Jul 22 '19 at 10:14
  • External configuration means ? I want to add Nationalized annotation to customer specific fields. – Vinothini Ponnusamy Jul 22 '19 at 10:20
  • I am not a Hibernate user, but I think you can override the annotations with XML files (of which you can have different versions for different customers). – Thilo Jul 22 '19 at 11:39

1 Answers1

0

If you want to modify your .java files, or any other file, using maven or ant you can call an external program/script to modify the files for you.

Maven pom.xml example:

<plugin>
  <artifactId>exec-maven-plugin</artifactId>
  <groupId>org.codehaus.mojo</groupId>
  <executions>
    <execution>
      <id>1234</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>${basedir}/scripts/modifyfiles.sh</executable>
      </configuration>
    </execution>
  </executions>
</plugin>

With ANT:

<target name="run">
   <exec executable="name-of-executable">
      <arg value="${arg0}"/>
      <arg value="${arg1}"/>
   </exec>
</target>

Eather way, you'll have to write an external script (create a runnable jar, python, shell script, exe...) to do what you want, like reading all .java files, looking for a string pattern, putting a string a line above...

Doing like this, your script will be called every time before compiling the source code.

res
  • 775
  • 8
  • 16