2

I run mvn dependency:analyze command to check the unused jar in my java project, part of the result as bellow:

[WARNING] Unused declared dependencies found: [WARNING]
org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:comp ile [WARNING]
org.springframework.boot:spring-boot-starter-test:jar:2.0.3.RELEASE :compile [WARNING]
org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.3.RELEASE :compile [WARNING]
org.springframework.boot:spring-boot-starter-actuator:jar:2.0.3.REL EASE:compile [WARNING] org.aspectj:aspectjweaver:jar:1.8.9:compile [WARNING]

But spring-boot-starter-test is actually used in src/test/java package,

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestClass {
...
}

I want to know why spring-boot-starter-test: appear in Unused declared dependencies found section.
Is there a way to find the exact jar which is not used in my java project

Gavin Gu
  • 61
  • 6

2 Answers2

0

If you declared the dependency without a <scope> tag, <scope>compile</scope> will be used. If you use it for testing only, you should declare it as <scope>test</scope>.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Hi dan1st:I've tried compile or test, and it still appear in Unused declared dependencies found, and beside unit-test class, some other jars such as spring-boot-starter-jdbc also has this problem – Gavin Gu May 07 '19 at 06:03
  • Part of Text fragment is paste as bellow : org.springframework.boot spring-boot-starter-test ${springboot.version} compile org.springframework.boot spring-boot-starter-jdbc ${springboot.version} – Gavin Gu May 07 '19 at 06:21
  • What happens if you delete the dependency? – dan1st May 07 '19 at 06:48
  • There are references in the code,and eclipse show compile errors – Gavin Gu May 07 '19 at 07:06
  • Are the references only in src/main/java (or the testing directory)? – dan1st May 07 '19 at 07:34
  • spring-boot-starter-jdbc are used in src/main/java and spring-boot-starter-test are used in spring-boot-starter-test. – Gavin Gu May 07 '19 at 07:39
  • If this is so, `spring-boot-starter-test` should be declared as `test` – dan1st May 07 '19 at 07:41
  • Hi dan1st, If I change the scope to test, and the whole spring boot jar will not be involved, and The project start will fail. I am wonder if I can use mvn dependency:analyze to check the jar which is not used in java project ,why is used jar appears in Unused declared dependencies found section? Is there something I missed? – Gavin Gu May 07 '19 at 08:11
  • Is your `spring-boot-starter-test` directory included in your spring boot jar? – dan1st May 07 '19 at 08:15
  • spring-boot-starter-test and spring-boot-starter-jdbc is a dependency in pom.xml. And The spring boot jars is included when I used these dependency – Gavin Gu May 07 '19 at 08:24
  • I mean the following: Is the code that references your test dependency in src/main/java or in a test folder(normally src/main/java)? – dan1st May 07 '19 at 08:25
  • The source is in src/test/java,and after mvn dependency:analyze ,the class is in target/test-classes – Gavin Gu May 08 '19 at 00:36
  • I think it should solve the problem if you change it to src/test/java. Test-classes should be in the test source directory. – dan1st May 08 '19 at 04:43
  • Dan1st, I've tried, but it didn't slove the problem. – Gavin Gu May 08 '19 at 05:38
  • Did you try it in combination with `test`? – dan1st May 08 '19 at 08:47
  • I tried scope with test and Test-classes should be in the test source directory, the problem still exist – Gavin Gu May 08 '19 at 09:03
  • Is the code, that uses these dependencies test code or actual production code? If it is only test code, you should be able to move the whole test code into `src/main/test` and set the scope of these dependencies to `test`. – dan1st May 08 '19 at 09:06
  • Yes, I just use these for test code and these code is already in src/main/test, and also I try set scope to test. And the problem is exist . The problem is now not only spring-boot-starter-test but also some other spring-boot-starter has the problem. I am suspect that it has something related to spring-boot-starter jars. – Gavin Gu May 08 '19 at 09:38
  • have you tried deleting the .m2 directory in you user home and run `mvn clean dependency:analyze`? – dan1st May 08 '19 at 09:44
  • Yes, I have tried this, but have no advanced,I find an other discuss which maybe helpful:https://stackoverflow.com/questions/37528928/spring-boot-core-dependencies-seen-as-unused-by-maven-dependency-plugin – Gavin Gu May 09 '19 at 01:15
  • Seem like spring-boot-starter tend to some job on runtime,which can not been scan by maven – Gavin Gu May 09 '19 at 01:21
0

I came across the same problem. Enventually this helped me, though for the sake of completeness this is what you can do:

Springboot does inspect the classpath and add dependencies on itself so you don't have to do it. For maven pedendency plugin this is intransparent and it seems that these dependencies are not used.

What I did to manage this:

  1. Run: mvn dependency:analyze It will print out something like this:
    [WARNING] Unused declared dependencies found: 
    [WARNING] org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
  1. Remove the dependency in your .pom file.
  2. Run mvn verify
  3. If the build fails due to missing dependencies, add this dependency as "usedDependency" to maven-dependency-plugin settings in .pom.
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>analyze</id>
                    <goals>
                        <goal>analyze-only</goal>
                    </goals>
                    <configuration>
                        <ignoreNonCompile>true</ignoreNonCompile>
                        <failOnWarning>false</failOnWarning>
                        <outputXML>true</outputXML>
                        <usedDependencies>
                            <usedDependency>org.springframework.boot:spring-boot-starter</usedDependency>
                        </usedDependencies>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Repeat this for every warning found by maven dependency plugin.

Joel Neukom
  • 529
  • 6
  • 7