SGodoy
SGodoy

Reputation: 763

ClassNotFoundException when running Maven project with Spring dependencies

I have these spring dependencies in my pom.xml:

[...]
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
[...]

But when i'm running the .jar i'm getting a ClassNotFoundException caused by org.springframework.core.io.Resource I have the spring-core dependency added, so i don't know what is it happening

[SOLVED] Adding maven-assembly-plugin to the pom.xml like that:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.project.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
</plugin>

And compiling with mvn compile assembly:single

Upvotes: 1

Views: 165

Answers (1)

Kamil Piwowarski
Kamil Piwowarski

Reputation: 524

If you want the dependencies to be linked in your jar file, one option would be to make sure classpath is set and dependencies are copied to a folder which you will need to bundle with the packaged app. You can do that by using maven-jar-plugin and maven-dependency-plugin, like in sample below:

<build>            
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.something.AppMainClassName</mainClass>
                        <classpathPrefix>dependencies</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/dependencies/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</build>

in this way all dependencies should be present in "dependency" directory inside your project build directory (for example: "target")

Upvotes: 1

Related Questions