I have a project in which I want to use classes generated by ANTLR4 in a piece of Kotlin code.
In pom.xml, ANTLR4 is configured as follows.
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.1</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
The generated classes are put into target/generated-sources/antlr4:
mvn clean package, mvn clean assembly, as well as rebuilding the project in Idea lead to the following error:
Note that the errors occur only in the Kotlin class Transpiler.kt, but not in the test.
How can I fix this (make sure that classes generated by ANTLR4 can be used in Kotlin code)?
Update 1: After moving the grammar file as suggested by #Bart Kiers and executing mvn clean antlr4:antlr4, the errors in Idea disappeared. However mvn clean antlr4:antlr4 install still results in build errors:
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure:
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser
It should work if you do the following:
move Deplorable.g4 to src/main/antlr4/com/dpisarenko/deplorable/ (note that you placed it inside src/main/antlr4/com.dpisarenko.deplorable/!)
run mvn clean antlr4:antlr4
if not already done, mark target/generated-sources/antlr4 as the "Generated Sources Root" (right click it in your IDE and select Mark Directory as)
If not, try using the latest ANTLR4 version: 4.9.1 (not just the tool and runtime, but also for antlr4-maven-plugin).
Related
I have a project with finalised version in pom files , lets say 12.3.45 .
I have built the code for this version some time ago already, all the built jars are in the local maven repo.
Then at some point I have run mvn clean, so all the target folders are being removed.
And now I want to execute some code, as quickly as possible, using mvn exec:java. Preferably without building anything, because why not? all the jars at some point were already built, and I know there were no code changes after that. How can I force maven to execute the code as fast as possible , not recompile anything, and just reuse the jars from the local repo?
Thanks.
If your artifacts are in a local or remote repository you can use them as dependencies.
You can use exec-maven-plugin's options includeProjectDependencies or includePluginDependencies to use them in java execution
https://www.mojohaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies. includeProjectDependencies option is enabled (true) by default.
You can execute exec-maven-plugin without building anything with mvn exec:java command
Instructions:
To run exec-maven-plugin you would need a main class to run. I assume you have one in your project. If you don't - you need to make a separate project with a main class.
Create a blank maven project.
In the project add exec-maven-plugin configuration. Set the mainClass
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pack.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Include you artifacts as dependencies to the project
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>myartifact</artifactId>
<version>12.3.45</version>
</dependency>
</dependencies>
Run mvn exec:java to execute com.my.package.MyMainClass main class from my.group.myartifact artifact
Edits:
includeProjectDependencies option is enabled (true) by default
I've got a plugin that I need to use in my build. This plugin, however, has a build-time dependency on another plugin and that dependency does not work post-Java-8.
Specifically, the plugin that I want to use has this in its pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${javaVersion}</version>
<scope>system</scope>
<systemPath>${tools.jar}</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<phase>test-compile</phase>
......
</execution>
</executions>
</plugin>
It's set to the test-compile phase, so it's only ever run when the plugin itself is being built, and not when it's being used.
The problem is that it's got the systemPath as part of the dependency, and this is set to something that is not resolvable using Java 9+. That in turn causes the build for the project using the plugin to fail with:
[ERROR] 'dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${tools.jar} #
So either I need to completely ignore the maven-antrun-plugin as part of my using this plugin, or I need to at least make the dependencies for it work.
Any thoughts?
Cheers
My Maven project with the JAXB2 plugin works without errors if I run
mvn clean install
but it always fails if I skip the clean and run
mvn install
In this case the generated classes are not generated again which is correct:
[INFO] No changes detected in schema or binding files - skipping JAXB generation.
But then I get an compilation error that the generated classes and packages can not be found when the rest of the static Java sources in this Maven project are compiled:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project [...]: Compilation failure: Compilation failure:
[ERROR] [...] package [...] does not exist
[ERROR] [... ]cannot find symbol
Here is the relevant part of my pom.xml (the rest is only dependencies):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc-core</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/xsd/core</source>
</sources>
<packageName>com.example.core</packageName>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<!-- ... and more <execution> -->
</executions>
Am I correct that the only solution is to separate the static sources and generated sources into different Maven modules? Or is there any other way?
There is a bug in jaxb2-maven-plugin v2.2 https://github.com/mojohaus/jaxb2-maven-plugin/issues/35.
This bug has been fixed in v2.3
My project uses sun.security.tools.keytool to generate certificate under JDK 1.8 and this package can be found in rt.jar. According to Introduction to the Dependency Mechanism, System Dependencies, I can add rt.jar as a dependency to my project:
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>rt.jar</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
I'm pretty sure Maven found this jar file. However when I import sun.security.tools.keytool.Main, it still generates an error. Moreover, the most strange thing is if I copy rt.jar into someplace and fill its path in pom.xml, it works! As soon as I switch back to use the original rt.jar, it fails!
Can anyone tell me how could this happen?
I created a Maven project and added the <dependency> of your question to its POM.
First I got:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[6,34]
package sun.security.tools.keytool does not exist
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[12,50]
cannot find symbol
symbol: variable Main
location: class igb.so.SO31353565
Then, according to Cannot find symbol (CertAndKeyGen) with JDK8, I added:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<fork>true</fork>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
to the POM and the compilation succeeded.
If you are using Gradle instead of Maven you can add this to your build:
compileJava {
options.fork = true
options.forkOptions.executable = 'javac'
options.compilerArgs << "-XDignore.symbol.file"
}
It worked for me! ;)
I solved it like this:
Add to JAVA_HOME/lib/rt.jar
Go to Eclipse Menu Windows -> Windows -> Preferences -> Java -> Installed JREs
Select jdk version. In me case jdk-11.0.4
Select Add External JARs.. and add JAVA_HOME/lib/rt.jar
I have a Maven project. When I try to build it with Maven, I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.
3.2:compile (default-compile) on project myProject: Compilation failure:
Compilation failure:
[ERROR] ClassA.java:[32,38] cannot access ClassB
[ERROR] class file for ClassB not found
ClassB is inside another artifact, and that artifact is in the local repository. In fact, I have no problems building this project with the m2eclipse Maven plugin. It's only when I run mvn compile that the build fails.
What do I have to do to build from the command line?
Eclipse might be bypassing your pom dependencies and 'helping' you by finding a dependency that is not in your pom. Then when you run from command line eclipse isn't there to help anymore. I would double check your pom that you explicitly state dependency. You can also try
mvn dependency:analyze
for more info.
That looks like you have defined a usual class within the src/test/java instead of the src/main/java area which is sometimes the problem cause the eclipse things behaves a little bit different.
add to pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>src\main\webapp\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>