exception NoClassDefFoundError from deploy spring project with maven assembly

£可爱£侵袭症+ 提交于 2019-12-25 17:43:34

问题


i'm trying to build an executable jar with dependencies for a spring project.

my pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>xxx</artifactId>
        <version>3.0.x-SNAPSHOT</version>
    </parent>

    <groupId>com.mindcite</groupId>
    <artifactId>mindcite-rdfizer-tool</artifactId>
    <version>3.0.4-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>compony :: project</name>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.2</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>conf</directory>
            </resource>

        </resources>



            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <wtpversion>2.0</wtpversion>
                    <buildcommands>
                        <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
                        <buildCommand>
                            <name>org.eclipse.ajdt.core.ajbuilder</name>
                            <arguments>
                                <aspectPath>org.springframework.aspects</aspectPath>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>org.springframework.ide.eclipse.core.springbuilder</name>
                        </buildCommand>
                    </buildcommands>
                    <additionalProjectnatures>
                        <projectnature>org.eclipse.jdt.core.javanature</projectnature>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptors>

                        <descriptor>assembly/package.xml</descriptor>
                    </descriptors>
                </configuration>

            </plugin>


        </plugins>
    </build>


</project>

my assembly descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>full</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <outputDirectory>/</outputDirectory>
            <useDefaultExcludes>false</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>icons</directory>
            <outputDirectory>icons</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

when i run i get exception: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext

what am i doing wrong?

i don't want maven to create a jar file with all the dependencies jar outside the lib folder.


回答1:


after playing with that for two days, the solution was: because i put all the dependencies jar in a lib file (in the assembly file)

<dependencySets>
    <dependencySet>
        <unpack>false</unpack>
        <scope>runtime</scope>
        <outputDirectory>lib</outputDirectory>
    </dependencySet>
</dependencySets>

i needed to create the class path myself, in the pom file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>

   <configuration>
        <archive>
            <manifest>
                 <mainClass>myMainclass</mainClass>
                 <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
            </manifest>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
         </archive>
     </configuration>
 </plugin>



回答2:


Add spring-context dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>2.5.2</version>
</dependency>

You can also declare the spring version in the properties on top of pom.xml and reference it, such as:

<properties>
    <spring.version>2.5.2</spring.version>
</properties>

And then use:

<version>${spring.version}</version>



回答3:


Adi,

You are missing org.springframework.context.support jar

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>2.5.2</version>
</dependency>



回答4:


From the assembly descriptor you provided, you explicitly defining that, it should generate all dependency into lib/ folder.

However, it is a very common way to put library jars into lib/ folder. And usually in that case, you will provide a simple script to start the program which include all the jars under lib/*.jar as classpath.

There is one more alternative, you can generate MANIFEST with classpath defined inside.

The exact solution is basically context dependent and most likely depends on how you would like to delivery the binary distribution.




回答5:


A ClassNotFoundException usually means that the class in question is not in the CLASSPATH. The class loader can't find it. You're assuming that something is in the CLASSPATH, but it's not. The solution is to check your assumptions and figure out how to make all the necessary classes available in the CLASSPATH at runtime.



来源:https://stackoverflow.com/questions/7627241/exception-noclassdeffounderror-from-deploy-spring-project-with-maven-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!