Prevent the download of Gradle and Maven dependencies - eclipse

Before asking my question I'd like to apologize if this is really a
simple question.
I'm trying to find out the way, where we can locate and reuse the dependencies of Gradle and Maven. I want to find the possibilities of reusing them within multiple projects at the same time, opened in STS, Eclipse or Intellij.
Instead of download them every time when we create a new project. It will save me the data, time and disk space.

Maven dependencies are not downloaded again every time you create a new project.
They are downloaded to the local repository of your computer once and then they are read from there. So as long as you open a new project on the same account, Maven does not redownload dependencies.
The exception are SNAPSHOT dependencies, for which Maven periodically looks for new updates in the remote repositories.

Gradle, like Maven, has an Offline Mode, which means you're telling it to always try to re-use cached dependencies instead of resolving them from network.
Each of the dependency manager has a command line option for this, but in IntelliJ IDEA you can enable/disable it simply by toggling an icon-button in the Gradle toolbar.
And the same for Maven.

Related

Fixing my Scala Intellij project when loading it from an existing project in sbt and dependencies download fails

Oftentimes I have troubles with sbt downloading its dependencies when behind a corporate firewall when trying to "Load a project from existing sources" -> sbt. It eventually times out and leaves my project in the following state:
there is some problem when
I can at this point download all my dependencies through the sbt-terminal tool (or the command-line) but Intellij will still keep this "view", instead of correctly showing me the diverse folders the project contains (like src/, test/, etc).
Is there a way to fix this?
Thanks
I also have had this problem multiple times. I usually close IntelliJ, clean its repository rm -rf .idea/, and then open the project folder (instead of importing it). It's always done the job for me.

How do I get my Eclipse-Maven project to automatically update its classpath when I change a dependency in my pom.xml file?

I’m using Eclipse Mars with Maven (v 3.3). When I update a dependency in my pom (change the version), my Eclipse-Maven project doesn’t pick it up, even when I right click my project, and select “Maven” -> “Update Project.” I know this because I do not see compilation errors in the Eclipse Java editor that I see when I build the project on the command line using
mvn clean install
When I remove the project from the workspace and re-import it, then things get back to normal. However this is a cumbersome process. How do I get my Maven-Eclipse project to automatically detect changes in my pom and update the project libraries appropriately?
And yes, in the “Project” menu, “Build Automatically” is checked.
When you import the project into Eclipse, use Eclipse's own built-in Maven support (aka, m2e). I recommend against using mvn eclipse:eclipse as it doesn't give the best results (as you're seeing). Maven is a build and dependency management tool, not an IDE; expecting it to manage IDE-specific stuff is silly, in my opinion (I realize the Maven team thinks differently, that Maven should be responsible for managing your IDE, but that's nonsense).
So if you have the project available on your system, delete any Eclipse-specific files (typically just .classpath, .project, and folder .settings), they were generated by mvn eclipse:eclipse and you don't want them interfering with the "proper" import process described here. Then inside Eclipse, use File > Import > Maven > Existing Maven Projects to import the project. That should result in better integration between Eclipse and maven, including automatically updating the Eclipse build path when the pom is changed.
As a quick check, after doing the import that way, you should see a group called Maven Dependencies in the Libraries tab of the project's Build Path (in Properties dialog). Like this:
If you want the Eclipse project configuration to be automatically updated every time the pom is changed, there's a (experimental) setting for that under Preferences > Maven. Be aware that doing so might not be desirable, though - as mentioned in this feature request, it's a somewhat lengthy process that touches a bunch of stuff in the Eclipse Project; doing that automatically on every pom.xml change could end up being more trouble than it's worth.
Three Mandatory checks you should do for automatic update in your classpath
Your Repository is not in-sync with your Eclipse IDE, Please check the below settings in your IDE.
Right Click your any POM.xml from your IDE and check for the Maven profile which should be auto-activated. Also offline and Force update check box shouldn't be enabled. Please refer the below image.
Always check for your user settings which should reflect your local maven settings.xml, as shown in the below figure.
After performing all these checks, refresh your Eclipse Work-space to get these changes reflected.
Eclipse should be updating your classpath. If it's not, that implies something is going wrong.
It's hard to say what the problem could be exactly without knowing more about your project's pom.xml. More information might be necessary to solve the issue, but I'll just make a stab in the dark:
Open the .project file in your project's root folder and check the ordering of builders and natures there. It might be possible that some other nature on the project is also causing maven2Nature to fail. Move maven nature up and see if that helps any.
Alternatively you might be thinking that Eclipse does not update your dependencies because it does not add some some error indicators in the project that should be there with new dependencies. If that's the case try cleaning the current project (project>clean...). Maven in Eclipse does not necessarily trigger a full rebuild when dependencies are updated.
If none of this works, closing/opening the project might solve the issue quicker than re-importing.
What you wrote, should work. Did you check this:
does "pure" mvn install from terminal see your changes in POM?
maybe some Maybe plugin is buggy, cached some dependencies in target, and mvn clean install is needed
you can run Eclipse in a new workspace, and import your project there, sometimes it helps in case of such strange problems
instead of importing Maven project to Eclipse via m2eclipse, you can try to create Eclipse files via the old mvn eclipse:eclipse and see what happens then
does it work well when you try to import your Maven project to other IDE, the free IntelliJ Community Edition for example?
As a last resort, you can delete your current Eclipse installation and install a new version. When you add several plugins, they might interfere with one another and create weird behavior. After you do that, do not import your Maven project into your workspace, but rather create a new one and copy and paste the files that you had.

Maven integration with IDEs

I thought I understood Maven as I've worked on a few projects using it, but I seem to be having trouble creating my own project with it, so can someone correct me wherever I may be wrong?
Assuming I create a new Maven project (we'll say I did it through Eclipse or IntelliJ):
If I add a dependency to my POM file, assuming I add a public repository where that dependency can be found, shouldn't the IDE download the specified JAR and make it so that it is recognized when I use it in my code? Currently, my projects do not recognize any classes that should be found in JARs via my POM dependencies.
Assuming #1 does work, how can I determine via maven which transient dependencies I have? For example, I'm using classes from Pentaho Data Integration, and some of the plugins for it reference things like org.mozilla.javascript.*. Could maven automatically tell me that without me having to run the code, see it fail, and manually fix the dependency? There will be hundreds of those instances here, so manual fixing really isn't viable.
Here are my IntelliJ two cents:
1 - Adding a dependency in pom.xml of your project
Should indeed download the depended jar(s). You may need to approve "Import Changes" dialog if it pops in, or enable auto import.
2 - Seeing transitive dependencies
It can be achieved via Maven Dependencies Diagram - unfortunately only in IntelliJ Ultimate edition. You can use Maven Dependencies Plugin to see the dependencies tree in your favorite CLI.
Question 1: Adding a dependency
In Eclipse, depending on how you created the project, you should be able to add dependencies that are automatically recognized using the maven context menu.
Note that you should have created the project using the eclipse maven plugin so that it has maven nature.
To add dependencies/plugins from a remote repository, you can search in the resulting UI for a dependency if you know the artifactId or groupId. The plugin will pull up the deps whether the repo url is specified in the pom.xml or not.
After adding a dependency to the POM, the IDE will start downloading it and all transient dependencies as soon as you save the file.
If something goes wrong, you can try to "Update Project" from the context menu.
Question 2: Determining transitive dependencies
Transient dependencies are visible in the "Dependency Hierarchy" tab of the POM editor.
I usually default to the command line because it allows much more flexibility and functionality when tracking the dependency graph.
I am sorry but I have not worked with IntelliJ

Keeping Eclipse project dependencies in sync with an external build system

Here is the situation. A development team has a large number (hundreds) of Eclipse projects. The code is very much in churn - new projects are being created; projects are being renamed and project dependencies are constantly changing. The external build system is ant. It is proving extremely challenging to keep the dependencies defined in the ant build files in sync with the state of the world in Eclipse. The external ant build needs constant changes to keep up. For various reasons, using ant as the default builder in Eclipse is not an option. The developers want to continue using Eclipse as the build and edit environment for local use.
Question: Is there a tool which will allow a single set of dependencies to be maintained which can be used by Eclipse as well as an external build system like ant?
I have heard of Gradle but never used it before. Would it make sense in this context? I am pretty sure Maven wouldnt work for what is needed
The typical workflow should be:
1. Developers continue working as they currently do - creating and changing Eclipse project dependencies at will and using the default Eclipse builder to compile and test locally.
2. Some mechanism exists by which these dependencies can be carried into an external build system like ant and an external continuous build triggered on every checkin.
Appreciate your feedback - thanks!
We have been quite successful at using Gradle to tackle a similar problem. Here's the outline of the setup
Each project contains a build.gradle that defines project specific dependencies and tasks (may even be empty).
A special master project contains build.gradle that sets up common dependencies and tasks for child projects, and/or injects settings pertinent to a group of child projects.
Logically master project is the parent project, but it exists as a sibling folder so that Eclipse can be more comfortable with it.
Gradle contains a built-in Eclipse plugin which allows generation of Eclipse settings files for each of the projects from the dependencies information (including inter-project dependencies). It works nicely for simple projects, and as for more complicated ones Gradle allows you to tinker with the settings files, so you can do pretty much everything. From here you have two options:
Not to store Eclipse settings file in the repository and call the generation task every time you do a fresh check-out (I prefer this option).
Tell Gradle to use custom variables to make it generate generic settings files which can be checked-in to the repository. You'll then only need to run the generation task when dependencies or other configuration changes.
(Optional) It's a little tricky, but you can make Gradle parse existing project ivy.xml files and set up dependencies from there. I had some success with this, although I would recommend converting dependencies into Gradle format for more flexibility.
Continuous build system integrate with Gradle very well (same as ant). If you are using Jenkins (Hudson) there is a Gradle plugin.
The advantage of using Gradle is that it scales pretty well, and you can support other IDEs like IntelliJ or Netbeans at the same time without much effort (unless you have lots of crazy custom settings). An advantage and a disadvantage is that Gradle is a powerful build system which requires learning Groovy and Gradle DSL which may take some time to acquire. Also the documentation is awesome.
Gradle has a very active community with the sole purpose of tackling exactly this kind of problem.
Hope this helps, and best of luck!
How about parsing the .classpath files, generate a dependency tree and start building from the root. What you need is a convention on the layout of your projects or an generic (ant-) buildfile that could be changed in each project, if needed (e.g. different project layouts). I´m not sure if Eclipse Tycho could be used for that, since it´s a maven plugin(s) to build eclipse plugins or projects. But it´s able to resolve the bundle and project dependencies against maven repositories and eclipse update sites.

maven classpath container refresh job

I get stuck with m2eclipse and maven. After adding some dependency (hibernate 3.4, but I guess that doesn't really matter) eclipse got stuck with a message like in the title. Removing the dependency from pom file didn't help either. Restarting eclipse, checking out this as a clean project from repo neither. Alwyas gets stuck on refreshing. :/
What can get (m2)eclipse stuck on refreshing the project? All ideas welcome.
Workspace resolution can really slow the dependency resolution down. Maven needs to partially calculate each project to determine the dependency hierarchy, the time taken to do this increases rapidly as the number of related projects increases. To limit this, close projects you're not working on or uncheck the option to resolve workspace projects (so Maven will resolve the dependencies from the local repository, this means you need to install the projects after each update though).
If that doesn't help, try one of these:
Are your Maven settings correct? It may be that the preferences are not pointing to your platform install so Maven is downloading the dependencies to your user home in the background - this can take ages. If so tweak those settings. Select Window->Preferences->Maven and check if it is using the embedded Maven or not.
If you are behind a proxy you will need to set the proxy settings in your preferences, though if the problem is intermittent it won't be that.
Sometimes the persisted container isn't updated, I've never been able to isolate exactly why. I've found that closing the project and reopening can prod the container back into life.