Eclipse not recognizing generated class - java

I have used Maven plugin to generate classes in my project. However even after trying padding it explicitly from project build path, eclipse is unable to recognize it. It says,
"AbcBaseListener cannot be resolved to a type"
Project auto build is also ON.

Check 2 things:
1)
The important hint here is "class folder". Eclipse expects .class files in there, source code will be ignored.
To fix this, add this plugin to your POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
and update the project (Maven -> Update ...).
The other option would be to manually add another source folder to the build path but that will get lost whenever m2e updates the project configuration from the POM.
2) Make sure your generated files are having package names;
Use below code inside your g4 file after grammar Abc;
#header {
package antlr4;
}

Related

Code generation on file change in IntelliJ

I'm facing the following situation: I have a JSON-like structured file in my resources folder. Whenever I change the file, I want to automatically invoke a process, preferably a Maven plugin goal that I created, which will turn the file into a Java class. I tried achieving this in IntelliJ with different lifecycle options in Maven but had no luck.
Is Maven the right path to go down or are there other options? What I want to end up with is some kind of plugin (preferably IDE independent), that can generate Java classes from those JSON-like structures after each file save (or change).
The plugin setting I tried, including different lifecycle options such has validate, generate-sources, etc.
<build>
<plugins>
<plugin>
<groupId>com.plugins</groupId>
<artifactId>schema2java-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<pathToSchemas>${project.basedir}/src/main/resources</pathToSchemas>
</configuration>
<goals>
<goal>generateClasses</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to set a build path from terminal

In Eclipse, I can right click on the project and then click on Build Path and Use as a source folder. How can I do this from terminal?
For context, I have a maven project in Eclipse which I can run properly if I do the above mentioned things. I want to do the same thing from terminal. What is the command to do this from terminal?
Please follow the below steps If you prefer to add some external folder as source folder through the terminal way. Eclipse treats the following folders as source folders by default in case of maven project:
src/main/java
src/main/resources
src/test/java
src/test/resources
This case applicable even when you are run the maven project through the command line interface from the project root. If you like to add any other folder as source apart from the above list, which can be easily achieved through the build-helper-maven-plugin. Just create the required source folder through the terminal and then add the folder entry in the below maven plugin.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>**some source directory you prefer**</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
for more about the plugin

Handle different source folder in Eclipse

The Project I am working on has the regular src/main and src/test folders, but they recently introduced src/integration-test, which is supposed to contain, as the name suggests, integration-tests.
Unfortunately I can't seem to make this work with Maven and Eclipse. I tried adding a source folder, but Eclipse is not able to resolve the dependencies needed for the integration-tests and fails to compile the test-classes. I also can't start the tests with JUnit.
The source folder was added to the build path, but it did not help.
All other developers in my team use IntelliJ and report that they don't have these issues at all. But it´s bound to work with Eclipse as well, right?
You should handle this with maven by adding the source folder to the build:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
In this example the build will add a src/integration folder where you can put other test classes.
For more information check the build helper plugin documentation.

how to create source.jar on maven install

I'm working in Sprint Tool Suite and I've created a maven project. When I right click on the project I can select Run as...Maven Install. This takes all my class files and packages them into a jar file as expected and also creates a copy to my central maven repository C:\Users\Owner.m2\repository
All that is fine, but I'd like it to also package up all the source files into a sources.jar file. I suspect I'll need to edit the pom.xml file for the project to make that happen, but haven't seen an example of how to do that. Please advise.
You have to add proper plugin to your execution cycle
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

Class not able to recognize maven dependencies

I am trying to create a maven project. I added the dependencies as jar files and importing them in my class. But the class doesn't recognize it.
I tried a couple of options and serached but couldn't resolve. Please guide.
Attached below is the screenshot of my project str
Try this:
Right click your project and select maven then click update project. Select the project and select force update of snapshots/releases option. Then refresh the project and build it (if you do not see compilation error after updating).
Since the jar is not under web-inf folder you are seeing this error.
There are several ways to resolve this issue
add below profile
<profile>
<id>qa</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Dependencies directory created in target folder
mvn install dependency:copy-dependencies

Categories