Suppose IntelliJ says that my project uses some version of the library, like this:
How to find out where classes from this JAR are actually referenced?
There does not appear to be a way to display dependency tree as you can with maven.
You can run Gradle dependencies task to see the dependencies. To do it in Idea you can configure a Gradle Run configuration with task dependencies:
Related
This sounds dumb but is there anyway for me to specify dependencies for my Java project like how I would in a package.json file so that someone else who was to download the project code from my GitHub repo, would be able to run it without any errors or missing libraries?
I have never tried using external Java libraries before, such as apache commons. The most I ever used was JavaFX but on a personal project level. My main concern is that if I were to push my code up to the repo and have someone else clone it. It might not run properly as the imported libraries are not downloaded.
Is there something similar to package.json dependencies where the person who runs the code would automatically download all dependency libraries and have it run on their system?
You can use Maven or Gradle for this purpose. Maven has pom.xml where you can specify all your dependencies. Similarly gradle has build.gradle which does the same job.
I have a Maven project, imported from Eclipse, where the dependencies are set to scope provided. When the project is deployed, the jars are deployed as well so that works fine.
While developing, however, I use a "debugging project" that calls the Maven project, and when it runs I get a bunch of Class Not Found errors when the Maven dependencies are set to provided.
If I change the scope of the Maven dependencies to Compile then the project works fine.
If I change the scope of the dependencies to compile, would that change the output of the project? i.e. add a bunch of jars? That would be undesirable.
I also tried to change the Debug Configuration settings and specified the Maven project in "Use classpath of module", but then the files of the debugging project are not found.
How can I specify the classpath to be of both the Maven project and the debugging project, so that classes from both projects including the dependencies will be on the classpath?
Thanks!
There are 3 types of dependency scope: compile, test, and provided,
compile: the dependency library will be used in all steps: compile , test and run,
test: the dependency library will only be used in the test
provided: the dependency library will only be used in compile and test, but in the run time, the dependency library must be provided by the container otherwise it will throw class no find issues.
Your issues is that you did not provide the dependency library in the run environment ( container) when running your project.
hope this can help you
How did you import the project to Idea? If the project is opened as a Maven projects, it should work out of box.
Can you try to open the project by selecting pom.xml?
I want to use Mockito in my Eclipse, and you can only install it through build programs such as maven or Gradle, I chose to use Gradle. I am a beginner in using Gradle, but I just need to install Mockito and be able to use it as a dependency.
You don't actually "install" Mockito. You just add it to your dependency management system. Since you're using Gradle, all you have to do is add it to your build.gradle file. I'm assuming you've started with a blank Gradle project or something like that, which included a default build.gradle file.
Somewhere in that file you'll declare all your project dependencies:
dependencies {
// other dependencies will be here, just add the following line:
testCompile("org.mockito:mockito-core:1.10.19")
}
You can find out Mockito's artifact/group ids and available versions here: http://mvnrepository.com/search?q=mockito
I'm hacking on a Maven-based project with a lot of dependencies; the project is normally meant to be developed in Eclipse but I need to work on it from the command line.
How to build+execute the project in a sane way? Something like mvn run, but of course Maven is not meant for running Java projects (for some reason).
The problem is specifying all the dependencies on java's commandline, I don't even know how to autogenerate that. I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me.
However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please?
Note: One possibility is running it in Eclipse once and capturing the java commandline. However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore.
Have a look at the maven exec plugin
mvn exec:java -Dexec.mainClass="com.example.Main"
if you do this frequently, you can of course configure it via plugin configuration.
Regarding finding out project dependencies - you can use maven dependency plugin
http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html
If you want to put them into file it'd be smth like
mvn dependency:list > dependencies.txt
See this question: How can I create an executable JAR with dependencies using Maven?. You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest.
I see three solution to this:
onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor
With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside.
Spring Boot Maven Plugin - but this is dedicated to Spring projects
Maven Assembly Plugin with custom descriptor. This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. After this you can simply execute your program or zip your jar with lib folder as distribution.
After this is possible to run your program on your computer or any other with one command:
java -jar myjar.jar
What I want to achieve is the following:
Checkout external project from svn
Replace spring.xml with my customized version
Change version of a dependency
Build
Package
Are there standard maven plugins to handle all these goals?
For svn operations, you can use: org.tmatesoft.svnkit:svnkit
For replacing spring.xml with a customized version: maven-assembly-plugin, maven-resources-plugin would both work, depending on what exactly you need to do (I'm assuming there's more work here).
For building and packaging, you can use the maven-invoker-plugin. That plugin basically lets you point at a project and run "mvn clean package" or whatever other maven commands you want such as invoking a plugin. It's pretty much just a way to run maven from maven.