How to reference sources in a separate IntelliJ project

How should I go about adding a link to the sources for a library if the source is part of another IntelliJ IDEA project? If I create a Path Variable for the library sources, Intellij will automatically update any paths in the project files (.iml files) of the library project to use the new path variable, which is problematic.

Some possible solutions which do not appear to be supported:

  1. Use an environment variable instead of a path variable. IntelliJ does not support using environment variables in file references (in .iml files or library .xml files).
  2. Limit the scope of a path variable to a single project. Path variables are global to all projects and all instances of IntelliJ. There does not appear to be any sort of per-project path variables.
  3. Force IntelliJ to not substitute a path variable for a given project. IntelliJ will always automatically update the paths in .iml files to use a path variable whenever possible.


As an example, I have a repository named /CollaboRhythm with a library /CollaboRhythm/CollaboRhythm.Shared. I also have a fork of the repository at /CollaboRhythm2. I have a plugin project named /DiabetesPlugin which has a dependency on /CollaboRhythm/out/production/CollaboRhythm.Shared/CollaboRhythm_Shared.swc and the sources /CollaboRhythm/CollaboRhythm.Shared/src. If I make a path variable COLLABORHYTHM_HOME for /CollaboRhythm, then /CollaboRhythm/CollaboRhythm.Shared/CollaboRhythm.Shared.iml will be updated to use COLLABORHYTHM_HOME (which is problematic). If I then change COLLABORHYTHM_HOME to point to /CollaboRhythm2, /CollaboRhythm/CollaboRhythm.Shared/CollaboRhythm.Shared.iml will now use /CollaboRhythm2/out/production/CollaboRhythm.Shared/ for the output folder instead of /CollaboRhythm/out/production/CollaboRhythm.Shared/

As a workaround I am currently placing my plugin project (DiabetesePlugin) in a subdirectory of CollaboRhythm (/CollaboRhythm/contrib/DiabetesPlugin) and then when I want to use the plugin with a different fork of the CollaboRhythm I have to move the folder (for example, to /CollaboRhythm2/contrib/DiabetesPlugin).

9 comments
Comment actions Permalink

Are you adding the dependency via a library definition? (See Help> Help Topics > Basic Concepts > Library)  If so, just attach the sources to the library definition. If not, I'd be curious why not and how you are adding the dependency. You can also add a module from another project as a module to the current project. That would be another way to add the dependency.

0
Comment actions Permalink

Yes, I am adding the dependency via a library definition and I have attached the sources to the library definition. However, the path to the sources is a relative file path. If the plugin project is a sibling to the /CollaboRhythm project containing the shared library, then switching to a different fork of the repository, or sharing the plugin project with my team is problematic because the library definition has to be updated with the new relative file path. IntelliJ path variables are intended to solve this type of problem but do not work well if the path you are referencing in the path variable is contained within another IntelliJ project.

0
Comment actions Permalink

Hi Scott,

Couple of questions regarding the problem:

  • am I right assuming that you have different forks in different file system directories and switching between them is performed via changing env variable value? (as opposed to git which has a notion of a single working directory and multiple branches kept at index);
  • the statement about 'switching to a different fork of the repository, or sharing the plugin project with my team is problematic because the library definition has to be updated with the new relative file path' is not clear - how do you want to preserve project setup at another team members machines if it's based on machine-local environment variables? Do you force the teammates to define that env variable at their machines as well?

Denis

0
Comment actions Permalink

Yes, I have different forks in different file system directories. One of my proposed solutions was to switch between them by changing an environment variable value. However, this does not seem to be supported in IntelliJ, so instead I am switching between them by moving the plugin project directory from a sub directory of one fork to another (I previously refered to the directory as contrib, but I renamed it to Ignored, as in /CollaboRhythm/Ignored/DiabetesPlugin and /CollaboRhythm2/Ignored/DiabetesPlugin). I added "Ignored/**/*" to .gitignore.

If I was going to use an environment variable, then yes, I would force my teammates to define that env variable on their machines as well. Obviously, this is not ideal because (a) it is harder to enforce and communicate the need to create an environment variable vs a path variable and (b) IntelliJ would probably need to be restarted any time the environment variable was added or modified. One of the other proposed solutions, such as alternatives 2 or 3 which I suggested above, would be better.

0
Comment actions Permalink

I created a ticket for that - IDEA-97801.

Btw, as far as I see you use git as a VCS - why do you have different repos instead of a single repo with a single working directory which content is auto-updated every time you check out a new branch?

Denis

0
Comment actions Permalink

I have separate repositories for certain plugins used with my application. The goal is to allow other developers create small plugin projects that are independent of the main application project. Also, in some cases, developers may choose to develop a plugin without having a copy of the main application repository.

0
Comment actions Permalink

A repo per-project/plugin idea is clear. But you mentioned earlier that you have different versions/forks/branches of the same project and keep them at different directories (git repos) where an env variable holds a path to a version to use. Why don't you use a single directory per-project and checkout target branch (git checkout) there whenever necessary then?

Denis

0
Comment actions Permalink

Sorry, I misunderstood. I have multiple Git repositories for the same application because the different repositories are owned by different organizations. When an organization wants to create a plugin for my application they can optionally create a fork of the application and then create a repository for their plugin. The plugin exists in its own plugin repository, and the plugin can either use (reference libraries from) the organization's fork of my application or the main repository for the application. Using a single repository for the application (single directory) is problematic because I want to have each organization own a separate fork, and I don't want to make the members of the other organizations contributors on the main application repository. I end up with copies of multiple copies of the application repository because I may want to test or contribute to the separate fork(s). A developer from another organization may similarly want to test or use multiple forked versions of the application. Perhaps there is a way to achieve what I need using branches, but if so I don't know how to do it.

0
Comment actions Permalink

It's clear now, thanks. AFAIK it's possible to achieve that via branches but it involves explicit branches manage (import/export) and is really less convenient than separate repos.

Denis

0

Please sign in to leave a comment.