0

I'm running antlr with maven. Antlr generates .java file from .g file and I need to post process generated java file (do some changes in it). How can I do it?

<plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr3-maven-plugin</artifactId>
            <version>3.5.2</version>
            <configuration>
                <outputDirectory>src/main/java</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>antlr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
  • First don't generate code into `src/main` keep the defaults and let the generated code into `target/generated-code` – khmarbaise Apr 01 '22 at 08:01
  • The other question is: Why do you need to post process those generated files? In which way and how? Also the question why using such an old version of Antlr? Why not using Antlr 4... ? – khmarbaise Apr 01 '22 at 08:14
  • @khmarbaise, we use antlr3 because it's old project. We need to edit generated file because of 'code too large' exception. https://stackoverflow.com/questions/6283980/why-my-antlr-lexer-java-class-is-code-too-large It's possible to move part of statics (like tokenNames) to another file. – Andrei Filipchyk Apr 01 '22 at 10:56

1 Answers1

0

Finally I've found a solution. You have to use maven-antrun-plugin, it can execute java code. Another problem was that there were no compiled classes yet in the process-sources phase (you need to run it before compile). So before executing the java class, you need to compile it or do as I did - store the already compiled class and copy it to target classes.

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <copy file="path\to\LogicsParserPostProcess.class"
                                      tofile="path\to\target\classes\LogicsParserPostProcess.class" />
                                <java classname="LogicsParserPostProcess" failonerror="true">
                                    <classpath refid="maven.compile.classpath"/>
                                </java>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>