How to set a build path from terminal - java

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

Related

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.

Eclipse not recognizing generated class

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;
}

How do I include a "resources" directory alongside (not in with) my built JAR/classes in Maven?

I'm using Maven in IntelliJ and I want to include a 'resources' directory in my build output alongside the JAR and/or in whatever directory IntelliJ uses as the cwd when running my project. I don't want these files in the JAR/Classpath, just alongside the final built project.
The idea is that then, my project can refer to files named things in src/ext-resources/foo.txt as resources/foo.txt.
I'm currently using the following instruction in pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/ext-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, this doesn't appear to actually make it accessible, and doesn't seem to be copying these files at all. At one point, my config resulted in the subdirectories of src/ext-resources being copied into target/classes for some reason, but they aren't being copied into target/resources no matter what.
How do I make maven copy this folder into my final output directory?

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