Reputation: 12820
I have been working in XCode and with Objective-C in the last few years, but I knew that once I would have to transition to Eclipse or the like for Java projects. Case is, I do not particularly like Eclipse, which led me to a new development environment, namely IntellijIDEA. However, I am not at all familiar with how I should handle my files and how to add external libraries, nor do I know what my CLASSPATH is supposed to be. Where do I keep these external libraries in my project? I am also working with Maven and I am not quite sure why to use dependencies. If I include the .jar in my Classpath - or whereever I store my resources, classes etc. - why are there dependencies ? I have got one working project, but I am not comfortable releasing or using it while not exactly sure what I have done there. I understand how I can add external .jar files in IntelliJ, but where do I add these external libraries in my project ?
This is how I have done it up to now:
Let's say I have downloaded a Maven project and I would like to use it in some other project. I use mvn install
and then (in my Module Settings) I add the .jar from the target folder as an external library. Does IntelliJ keep a reference to that .jar file ? If that .jar is in my Downloads folder and I would like to transfer the project to a server, is this connection broken ?
Upvotes: 0
Views: 1134
Reputation: 933
in a Maven project you add your libraries you depend on in your pom.xml file, there is no need to add them somewhere else in your IDE and/or link them via the IDE to your project. just add a new dependency to your pom.xml
file.
maven stores (if not set otherwise) all the artifacts (libraries) in a folder: ~/.m2/
, that is where you keep your dependencies, and which is your local maven repository.
Maven makes your life easier as you do not have to add the jar's to your classpath anymore. make sure that your maven project is indeed recognized as a maven project by your IDE (usually it's a tiny M in your projects view).
here is a short intro to maven, that helped me a lot in the beginning: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html , especially in order to understand the lifecycle.
as you mentioned mvn install
: the artifact you built will get deployed to your local maven repository, all other projects you have can make use of this artifact and will reference the latest artifact you deployed.
you can run your project just as any other project, click on the run button and select your main class.
if your project results in a runnable jar, you can also define it in your pom.xml
file. see How can I create an executable JAR with dependencies using Maven? to create a jar containing all your dependencies/libraries in one jar, or How to create a runnable jar with maven that includes dependencies in a separate "lib" folder (not an uber jar) for a runnable jar which puts the deps/libs to a separate folder. anyway, in both cases the MANIFEST
file (which is needed for an executable jar) will be generated by Maven appropriately.
Upvotes: 3
Reputation: 57421
Try mvn idea:idea
to generate project for IntellijIDEA from the maven's pom.xml. Then just open the project and see all the libraries added.
Upvotes: 1