Projects and Libraries confusion (question)

(Reposted as a question, so answerers can earn points)

IntelliJ IDEA 11.1.1:

Some puzzles with Projects, Modules and Libraries.

(1)  From the Docs Basic Concepts > Project Structure I thought I  understood that a Project was a way to manage, in coordinated manner, a  number of individual Modules, where each Module could produce its own  output (eg: jar).  Yet when I create a Project it has settings that look  like it is a module in its own right, including having an src folder.  This seems a bit confusing.  Is the idea here that one can choose to use  either the Project-level src folderto build a single output, or ignore the Project's src folder and create multiple Modules with their own src folders etc?   Or is there some scenario where you would use both the Project-level  src folder and some subsidiary Modules, with outputs from both the  Project level and the individual Modules?

(2)  I expected to be able to set various settings at the Project level, and  have those be inherited by child modules. So if I set an external  Library for the Project, I expected it to be associated with the child  modules as well.  But that doesn't seem to work -- it seems to be  necessary to add external libraries to modules individually.

Am  I missing something here? Are project settings *not* inherited by child  modules?  If that's the case, what is the advantage of a Project?

(3)  Given the issue just described, in "Project Structure" dialog, Project  Settings > Libraries, is there a way to view or edit which modules  each library applies to?

(4)  How do I add sources for libraries?  I looked all over the Project  Structure dialog, and right-clicked everything, but couldn't find a way  to add sources. (I did stumble on the Attach Sources dialog via a  hyperlink that appears in the editor for files that lack sources, but  that seems the long way to get there, no?)

Thanks,

-- Graham

2 comments
Comment actions Permalink

(1) When you create a project, the settings such as the source directory are applied to the first module that is created in the project. By itself, the project does not have any source directory.

(2) There is no "inheritance" concept in play here. You can define a library on project level, which makes it available to all modules in the project, but you still need to add it to the dependencies of each module that uses it individually.

Also, because of that, the question of "advantage of a Project" is moot - project and modules have completely different sets of settings.

(3) On a library, you can press Find Usages (Alt-F7) to see which modules use it. To add or remove a library to the dependencies of a module, you need to select the module, not the library.

(4) The "Add" button in the library definition allows you to add any kind of roots (classes, source or javadoc) to a library. IntelliJ IDEA automatically analyzes your selection and detects how this should be processed.

0
Comment actions Permalink

Again, thanks Dmitry for the quick answer.

(1) When you create a project, the settings such as the source directory are applied to the first module that is created in the project. By itself, the project does not have any source directory.

Here's where I was confused: IDEA's New Project wizard has a panel which asks "Please specify a directory where source files for your project can be found", and the default relative path shows 'src'. That creates a directory called 'src' at the top level of the project.

That dialog also allows you to choose "Do not create source directory", but that seems like a bad choice, because the description says "Note, the program will recognize only those source files, that are located under this directory".

I have since discovered that this last statement is incorrect. If I go on to create subsidiary Modules, with their own src directories, then source can be located there.  It turns out that statement refers only to Project-level source code, and not to Module source.  So when I create a project that has no project-level module, I might as well omit that top-level src directory.

(2) There is no "inheritance" concept in play here. You can define a library on project level, which makes it available to all modules in the project, but you still need to add it to the dependencies of each module that uses it individually.

I see that now. "Make it available to all modules" doesn't actually make the library visible to the modules's code, it only makes the library something that the individual modules can optionally attach.

(3) On a library, you can press Find Usages (Alt-F7) to see which modules use it. To add or remove a library to the dependencies of a module, you need to select the module, not the library.

OK.

(4) The "Add" button in the library definition allows you to add any kind of roots (classes, source or javadoc) to a library. IntelliJ IDEA automatically analyzes your selection and detects how this should be processed.

Ahhh, the [+] button.   I never would have found that. I had tried right-clicking on the library item in the second column, and on the library jar in the right panel, but didn't realize that the concept here was to "add item to the library".  I had not understood that in this context "library" is not the actual library, but rather this project's info-about-the-library.  OK, now this makes more sense.

Thanks again,
-- Graham

0

Please sign in to leave a comment.