Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ cannot load sources from local Maven repository

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?

like image 350
Alex Jansen Avatar asked Jun 26 '14 20:06

Alex Jansen


2 Answers

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
like image 199
hawkeye Avatar answered Oct 22 '22 17:10

hawkeye


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

like image 2
dazito Avatar answered Oct 22 '22 15:10

dazito