11

I'm doing some testing work that requires the use of features in JUnit which are unfamiliar to me. In order to better understand these features I'd like to be able to view the JUnit sources inside IntelliJ alongside my project.

This project uses Maven. I have the following dependency for jUnit listed in my pom.xml file:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>

When I run "mvn clean install" on the command line the jUnit sources are downloaded from my company's Maven repository into my local Maven repository (.m2 directory). The project then compiles and runs without issue.

After right-clicking on the pom.xml file and selecting Maven->Reimport I can see that the classes, sources, and javadocs for jUnit are also present in the library settings in IntelliJ:

Sources visible in IntelliJ library settings

However, when I try to open a jUnit class file in IntelliJ and click on the "Download Sources" link I see this:

Sources missing in IntelliJ when viewing class

It seems to might like IntelliJ should be finding these sources just fine locally. Even if it did have to download them from my company's repository I also believe it should find them there since that's where the junit-4.10-sources.jar file in my local repository originally came from.

What might be keeping IntelliJ from loading the sources from the JAR file that it already knows about?

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Alex Jansen
  • 1,455
  • 5
  • 18
  • 34
  • There is a v good answer available to this question: consider commenting why/why not it has not yet been accepted (and *do* accept it if it were addressing your post) – WestCoastProjects Nov 11 '16 at 16:56
  • While the provided answer by @hawkeye was helpful, it did not resolve my issue and was not comprehensive enough to warrant accepting as an official answer at the time. This issue is also a couple of years old now and AFAIK has been solved in newer versions of IntelliJ. EDIT: Just noticed that you ARE still encountering this issue. If you happen to find a definitive solution to the problem then feel free to post your answer here and I'll accept that! – Alex Jansen Dec 04 '16 at 06:43

2 Answers2

19

I was able to replicate this in IntelliJ 16 on Windows.

Here is how I resolved it:

  1. Choose file -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Repositories
  2. Select Your local repository from the list (or add it if it is not there)
  3. Click the 'Update' button
  4. Click ok
  5. Right click your pom.xml file and select Maven -> Reimport
hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • 1
    How did you ever find out about that? – WestCoastProjects Nov 11 '16 at 16:56
  • I did follow these instructions - and the Local Repo *was* out of date. But even after each of these steps the result was "Unable to download source files" – WestCoastProjects Nov 11 '16 at 16:58
  • The assumption is you're also running a 'mvn compile' step on the command line on your pom.xml to update your repo – hawkeye Nov 11 '16 at 20:29
  • yes -and the sources file was confirmed to be in the repo and includes the specific classes. But no dice. – WestCoastProjects Nov 11 '16 at 20:48
  • Ok - for you it is time to start from scratch. Delete your maven and IntelliJ installs. Reinstall maven and create a whole new project with source. Compile that then install IntelliJ and try again. Then go back to your original source project. – hawkeye Nov 11 '16 at 21:53
  • well that's overkill without much sense to it: no reason to suspect new IJ/maven installs would have effect. What I *will* do is delete the project and rebuild it. – WestCoastProjects Nov 12 '16 at 00:31
2

I had the same problem. In my case I have 2 projects developed locally (project A and B) where project B is a dependency of the project A. I solved it by:

First: On project B add the following plugin to your pom file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>

More info about this plugin here: https://maven.apache.org/plugins/maven-source-plugin/usage.html

Second: On project B root folder run: mvn source:jar followed by mvn install. The first command will generate the sources and the second will publish them in your local repo.

Then IntelliJ, on project A, automatically picked up the sources from project B. If it doesn't then you may need to reload your project dependencies on project A or run mvn clean install on the root folder

dazito
  • 7,740
  • 15
  • 75
  • 117