Eclipse deletes external JARs from build path when Gradle dependencies are refreshed - java

I have a problem with Eclipse: whenever I click Gradle > Refresh Gradle Project to refresh my Gradle dependencies, Eclipse deletes all of the external JARs in my build path. This happens with every Gradle project, and reinstalling Eclipse doesn't fix the problem.
The "Refresh Gradle Project" option:
Before and after refreshing:
Any idea how to fix this? Thanks in advance.

Yes, that is the expected behavior: Refresh Gradle Project resets the dependencies to the ones specified in the build.gradle file. So it overwrites your manually made changes in the Java Build Path.
In a Gradle project, instead of changing the Java Build Path settings directly, add all dependencies via the build.gradle file.

I can not post comment yet.
Did you check the gradle setting for repo locations ?
Eclipse may not able to get the jars from the repo.
and also did you try
gradle eclipse
to refresh the eclipse dependencies from command line, and refresh the project in eclipse.

Related

Trying to import maven project but some dependencies are missing in eclipse after the import

I have tried everything but nothing worked for me.
Some of maven dependencies are missing in the eclipse but are present in the pom.xml file and local repository
Any help would be much appreciated
Error Photo
Assuming you correctly imported the project, the problem could be that you don't have the repositories defined that contain the dependencies you want to use.
See https://maven.apache.org/guides/introduction/introduction-to-repositories.html for more information on how to define your repositories.
Ideally When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository but I ran into the similar problem before and updating the maven project did the trick for me project>maven>update project
below I have explained whole thing from build to updating with screen shots hoping it helps you
right click project> run as> maven build
above step should launch edit configuration and launch window, here set Goals: clean install
then click run
again right click on project>Maven>update project
check force update of snapshot/releases and click ok
finally, check under maven dependencies again if jars have downloaded
P.S. if you have JARs downloaded locally you can add them manually to your build path right click project>properties to resolve the issue

Problem updating an external jar in eclipse using gradle

I am having trouble deleting an external dependency from my build path. I have searched this site and the web but have not found a good answer.
I use gradle 4.9 and eclipse 4.7.3a to manage my automated tests. I have placed external jars in a folder named artifact in a git project's root directory.
My build.gradle file contains this line in the dependencies section:
compile fileTree(dir: "./artifact", include: '*.jar')
Well and good, it all works. However, when I try to delete a dependency using
Build Path > Configure Build Path... > Libraries > Project and External Dependencies
all the buttons, including Remove, are grayed out.
I can delete the file from the artifact directory and issue
gradle cleanEclipse
from the command line but when I open Eclipse and try to open the project (under Project Explorer) I get 'Open Project' has encountered a problem. The project description file (.project) for 'project name' is missing.
After replacing the old jar with the new version (different file name of course), I tried running
gradle
which is supposed to recreate configuration files deleted by cleanEclipse. No joy. Next, I tried running
gradle build
But after restarting eclipse, still a failure. The only thing that worked was to delete the project from eclipse and then to re-import it.
Is there an easier way?
Thank you.
Well, I found an answer.
After deleting and copying in the new jar file into the artifact directory, in eclipse, I selected Project > Clean ...
Then I right-clicked on the project and selected Gradle > Refresh Gradle project
I guess writing the question got me thinking about how eclipse handles .project files. Maybe this will help the next person.

Eclipse project not reading depdencies inside the POM file

In Eclipse Neon, I did an 'import existing maven project' from a local folder, whose content was copied from another machine. The project is not reading the dependencies on the POM file, and one example is the log4j dependency. A workaround is to add the log4j-1.2.17.jar manually to the project as an external jar, but this is wrong. Does anyone have tips on how to troubleshoot? I am fairly inexperienced with maven so I m not sure if there are additional settings or configuration that I have do when importing an existing project into eclipse.
You can make sure if your project dependencies are being pulled correctly or not by executing
mvn clean install
from within the project directory on the command line. This would download the required dependencies of the project as well.
The most common cause I have seen for this issue occurs whenever Maven encountered an error while downloading an artifact from a repository. In this case, Maven marks the download as failed and won't retry until some time elapses (I do not remember how long).
The easiest way to test this is to delete the .m2/repository folder in your home directory and let Maven download the artifacts again. If you have Maven installed you just execute "mvn install" from a console. Otherwise, you can try:
Right click the project in Eclipse
Select "Maven > Update Project"
Check "Force Update of Snapshots/Releases"
Click "OK"

Eclipse fix project setup by updating pom?

In an eclipse project, I referenced a class that was currently not in project's build path.
In the context sensitive pop-up that opens by hovering the mouse over the error, "Fix project setup" option has offered me to add a jar to the build path of the project. This jar is in my local maven repository.
However, the "correct" way to fix it is obviously by adding the relevant dependencies to my project's pom, instead of adding this jar to the build path of the project.
So is there a way to configure eclipse, so that it won't offer to add a maven repo jar to the build path, but it will offer to fix the project's pom?
This feature used to exist in m2eclipse (the predecessor of m2e), see e.g.
https://blog.sonatype.com/2010/03/adding-dependencies-using-m2eclipse/ which shows a "Search dependency for XXX" quickfix above the "Fix project setup..." quickfix.
Unfortunately it didn't make it into m2e, see https://www.eclipse.org/lists/m2e-users/msg01130.html
Make sure:
your project is fine outside of eclipse using only maven/pom, mvn
clean install (or similar) in an external shell/console
your eclipse project has maven nature
Eclipse -> Project -> Build Automatically is checked
Refresh your eclipse project
Do something in the pom.xml (save file) should trigger a build

How to build and run Maven projects after importing into Eclipse IDE

I am learning building a Java project in Eclipse using Maven. I created a Java project HelloWorld from
“maven-archetype-quickstart” template in a folder D:/maven_projects. Then to convert the Maven project to support Eclipse IDE, I navigated into the project folder and issued the commands:
mvn eclipse:eclipse and mvn package .
Then I imported the project in Eclipse and did the necessary Eclipse configurations like setting the Maven local repository in Eclipse classpath. Now the project in D:/EclipseWorkspace folder. I ran the project successfully in Eclipse printing "helloworld".
Now if I want to go on develop the project and for that reason want to add new dependencies in pom.xml in Eclipse, then the new jars are not added in classpath when I run the project.
So my question is after importing a Maven project into Eclipse how can I add more and more dependencies in pom.xml, then build and run the project? What is the recommended and efficient way to do this?
I would recommend you don't use the m2eclipse command line tools (i.e. mvn eclipse:eclipse) and instead use the built-in Maven support, known as m2e.
Delete your project from Eclipse, then run mvn eclipse:clean on your project to remove the m2eclipse project data. Finally, with a modern version of Eclipse, just do "Import > Maven > Existing project into workspace..." and select your pom.xml.
M2e will automatically manage your dependencies and download them as required. It also supports Maven builds through a new "Run as Maven build..." interface. It's rather nifty.
1.Update project
Right Click on your project maven > update project
2.Build project
Right Click on your project again. run as > Maven build
If you have not created a “Run configuration” yet, it will open a new configuration with some auto filled values.
You can change the name. "Base directory" will be a auto filled value for you. Keep it as it is. Give maven command to ”Goals” fields.
i.e, “clean install” for building purpose
Click apply
Click run.
3.Run project on tomcat
Right Click on your project again. run as > Run-Configuration.
It will open Run-Configuration window for you.
Right Click on “Maven Build” from the right side column and Select “New”.
It will open a blank configuration for you.
Change the name as you want. For the base directory field you can choose values using 3 buttons(workspace,FileSystem,Variables). You can also copy and paste the auto generated value from previously created Run-configuration. Give the Goals as “tomcat:run”. Click apply. Click run.
If you want to get more clear idea with snapshots use the following link.
Build and Run Maven project in Eclipse
(I hope this answer will help someone come after the topic of the question)
Dependencies can be updated by using "Maven --> Update Project.." in Eclipse using m2e plugin, after pom.xml file modification.
Just install the m2e plugin for Eclipse. Then a new command in Eclipse's Import statement will be added called "Import existing maven projects".
answer 1
Right click on your project in eclipse
go to maven -> Update Project
answer 2
simply press Alt+F5
after updating your pom.xml. This will build your project again and download all jar files
Right Click on your project
Go to Maven>Update Project
Check the Force Update of Snapshots/Releases Checkbox
Click Ok
That's all. You can see progression of build in left below corner.
When you add dependency in pom.xml , do a maven clean , and then maven build , it will add the jars into you project.
You can search dependency artifacts at http://mvnrepository.com/
And if it doesn't add jars it should give you errors which will mean that it is not able to fetch the jar, that could be due to broken repository or connection problems.
Well sometimes if it is one or two jars, better download them and add to build path , but with a lot of dependencies use maven.
If you are getting this error :in cucumber Exception
Cucumber Exception: java.lang.ClassNotFoundException:
cucumber.io.ResourceLoader :
then add following jar file to your pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.1.8</version>
</dependency>
Run the maven build once and should be gone

Categories