About this document

This document describes how to use jflex-maven-plugin, a Maven 3 plugin to call JFlex.

JFlex is a parser generator. Given a grammar, JFlex generates Java (TM) code to parse documents that follow this grammar. This document is not intended to be a tutorial on the use of JFlex; for more information, consult the JFlex user manual

Usage

Overview

This plugin reads JFlex grammar definition files (.jflex) and generates a corresponding Java parser (in target/generated-source/jflex by default).

Minimal configuration

This configuration generates java code of a parser for all grammar files (*.jflex, *.jlex, *.lex, *.flex) found in src/main/jflex/ and its sub-directories. The name and package of the generated Java source code are the ones defined in the grammar. The generated Java source code is placed in target/generated-source/jflex, in sub-directories following the Java convention on package names.

pom.xml

<project>
  <!-- ... -->
  <build>
    <plugins>
      <plugin>
        <groupId>de.jflex</groupId>
        <artifactId>jflex-maven-plugin</artifactId>
        <version>1.6.1</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <!-- ... -->
  </build>
  <!-- ... -->
</project>

More complex configuration

This generates the source for two grammars (src/main/lex/preprocessor.jflex and /pub/postprocessor.jflex) as well as all grammar files found in src/main/jflex (and it sub-directories). The generated Java code is placed into src/main/java instead of target/generated-sources/jflex.

in pom.xml

      <plugin>
        <groupId>de.jflex</groupId>
        <artifactId>jflex-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <outputDirectory>src/main/java</outputDirectory>
              <lexDefinitions>
                <lexDefinition>src/main/jflex</lexDefinition>
                <lexDefinition>src/main/lex/preprocessor.jflex</lexDefinition>
                <lexDefinition>/pub/postprocessor.jflex</lexDefinition>
              </lexDefinitions>
            </configuration>
          </execution>
        </executions>
      </plugin>

More documentation on the configuration options can be found in the description for the generate goal.

Even more complex configuration, using several executions

This generates the source for

  • all files found in src/main/lex/, using strict JLex compatibility.
  • and all files found in src/main/jflex, in verbose mode.

in pom.xml

      <plugin>
        <groupId>de.jflex</groupId>
        <artifactId>jflex-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <id>strict jlex</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <lexDefinitions>
                <lexDefinition>src/main/lex</lexDefinition>
              </lexDefinitions>
              <jlex>true</jlex>
            </configuration>
          </execution>
          <execution>
            <id>jflex</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <lexDefinitions>
                <lexDefinition>src/main/jflex</lexDefinition>
              </lexDefinitions>
              <verbose>true</verbose>
            </configuration>
          </execution>
        </executions>
      </plugin>

More information in the POM reference guide on plugins.

Which version of the plugin is best for you?

  • jflex-maven-plugin-1.6.1 depends on JFlex 1.6.1 and requires Java 5 when you mvn jflex:generate
  • jflex-maven-plugin-1.6.0 depends on JFlex 1.6.0 and requires Java 5 when you mvn jflex:generate
  • jflex-maven-plugin-1.5.0 depends on JFlex 1.5.0 and requires Java 5 when you mvn jflex:generate
  • maven-jflex-plugin-1.4.3-r1 depends on JFlex 1.4.3 and requires Java 1.3 when you mvn jflex:generate
  • maven-jflex-plugin-1.4.2-r1 depends on JFlex 1.4.2 and requires Java 1.3 when you mvn jflex:generate
  • maven-jflex-plugin-1.0 depends on JFlex 1.4.1 and requires Java 5 when you mvn jflex:generate
  • maven-jflex-plugin-0.3 depends on JFlex 1.4.1 and requires Java 1.4 when you mvn jflex:generate