0

I've created a new Spring Boot project to learn how to include beans which I've declared in an external jar file. I've created a file named ContextApplication where all I want to do is display all those beans that have been scanned by Spring. Note that there isn't a single xml file involved in this process. I like that and would prefer to keep it that way. However, if we have to, I'll try just about anything at this point.

ContextApplication:

package com.anypackage.jedcontext;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.tacticalenterprisesltd"})
public class JedcontextApplication {

    public static void main(String[] args) {
        SpringApplication.run(JedcontextApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }
}

When I run this, it certainly works and provides a list of known beans, however, as part of the project, I've referenced a jar file jed-1.6.jar which has other classes in it marked as @Component or @Service.

enter image description here

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anypackage</groupId>
    <artifactId>jedcontext</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>jedcontext</name>
    <description>An application context test</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.tacticalenterprisesltd</groupId>
            <artifactId>jed</artifactId>
            <version>1.6</version>
            <systemPath>C:\Users\Owner\Documents\workspace-sts-3.8.4.RELEASE\JED\jed-1.6.jar</systemPath>
            <scope>system</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Now, when I run the application, the marked classes within the jar file are not being scanned and not showing up in the list in the console. Even though I explicitly provide this annotation: @ComponentScan(basePackages = {"com.tacticalenterprisesltd"}) over the ContextApplication class to scan everything within the jar file. This would include any sub packages I'm hoping. So, the bottom question here is, "Why isn't Spring reading in the classes within the jar file and how do I make it do so?" Please advise.

Alan
  • 822
  • 1
  • 16
  • 39
  • Your `jed.jar` should be a maven depend3ncy not added randomly to the class path. Also is that jar a regular jar or another jar created with Spring Boot. – M. Deinum Jun 13 '17 at 17:43
  • jed-1.6.jar is a library I created. I tried adding the jar file under "Maven Dependancies" as seen in the image above, but couldn't. Everytime I select Add Jars or Add External Jars, the jar file goes to the top of the list, not under Maven Dependancies. – Alan Jun 13 '17 at 17:55
  • Ofcourse it wouldn't. That jar has to be in your maven repository and added as a `` to your `pom.xml`. – M. Deinum Jun 13 '17 at 17:57
  • How do I get the jar in under Maven Dependencies? – Alan Jun 13 '17 at 18:02
  • Just like other dependencies using the `` tag in your `pom.xml` (as stated in my previous comment). – M. Deinum Jun 13 '17 at 18:06
  • OK. Managed to add to pom.xml the jar file. Ran the application and still the beans within the jar are not appearing in the list to the console. – Alan Jun 13 '17 at 18:25
  • How did you add it (add your `pom.xml` to your question), also you haven't answered the question if the jed jar is a regular jar or a spring boot jar. – M. Deinum Jun 13 '17 at 18:33
  • system dependencies aren't going to work as those aren't included in the class path (just like `provided` dependencies when running the application). See https://stackoverflow.com/questions/10935135/maven-and-adding-jars-to-system-scope for more information. – M. Deinum Jun 13 '17 at 18:40

1 Answers1

1

You should use includeSystemScope parameter to include your jar to classpath of the SpringBoot app

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <includeSystemScope>true</includeSystemScope>
  </configuration>
</plugin>

Or as another option you can install your jar to the local maven repository

mvn install:install-file -Dfile=path/to/jed-1.6.jar -DgroupId=com.tacticalenterprisesltd -DartifactId=jed -Dversion=1.6 -Dpackaging=jar

And then use it as a standard maven dependency

   <dependency>
        <groupId>com.tacticalenterprisesltd</groupId>
        <artifactId>jed</artifactId>
        <version>1.6</version>
    </dependency>
Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • I see where anyone might be confused by the package naming so I redid the project with a totally different package name to avoid confusing Spring. Notice the edit above. When I run the app, Spring is still not reading in the files within the jar file jed-1.6.jar. So we're back to square one. – Alan Jun 13 '17 at 16:24