开发者

ClassNotFound exception while using the Maven Shade Plugin

I am trying to follow this link: http://maven.apache.org/plugins/maven-shade-plugin/examples.html

I am new to Maven. I feel a bit out of depth trying to follow the example.

I am able to get Quartz Scheduler to get working with Spring. I want to be able to run it from commandline using jar file.

Here are the list of classes and pom file I used.

ClassNotFound exception while using the Maven Shade Plugin

EDIT:

I am able to get a shade jar file. I used mvn clean install

but when I try to run it from the command line, I get the following errors.

C:\Users\SpringExample\target>java -jar SpringExample-1.0-SNA PSHOT-shaded.jar Exception in thread "main" java.lang.NoClassDefFoundError: org/sonatype/haven/Ex odusCli Caused by: java.lang.ClassNotFoundException: org.sonatype.haven.ExodusCli at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: org.sonatype.haven.ExodusCli. Program will exit.

EDIT2:

I used the following in the pom above using this link:

http://seanfreitag开发者_C百科.wordpress.com/2011/07/25/create-an-executable-jar-with-dependencies-using-maven/

 <project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
                    <Build-Number>123</Build-Number>
                  </manifestEntries>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>


I haven't used Shade, but I suspect that:

  • the pom that you are using to invoke Shade is not shown

  • the Maven example showing how to set a Main-Class assumes that the class org.sonatype.haven.HavenCli is somewhere in the jar being assembled

  • you have no such class

  • you should change the class name in your <mainClass>org.sonatype.haven.HavenCli</mainClass> to whatever you want to use as a main class


UPDATE: You need to specify the Main-Class attribute in the Manifest.mf in your jar. See the example "Shade Plugin where a Main-Class is added to the MANIFEST.MF" for that.

--

Yes, you should embed the plugin code to your pom file like the following

<project>
       <!-- Other tags -->
        ---
    <build>
      <plugins>
        <plugin>
           ---
        </plugin>
      </plugins>
    </build>`
</project>

You have probably included the <plugin> as a direct child of <project> in the pom file. It doesn't work.

The usual way of building maven projects with mvn install (or mvn package) will create the shaded jar if the shade plugin configuration are specified. So, there will be two jars; the original jar and the uber jar.

exclude: Generally, the uber jar will include all the classes in the dependencies jar list in the pom. the excludes specify a set of jar files that need NOT be in the shaded jar. If you have a closer look at the example, it excludes the junit:junit jar, which means that classes in the junit will not be in your uber jar.


Add following snippet to your plugin. This should help.

 <artifactSet>
    <includes>
        <include>org.sonatype.haven.ExodusCli:*</include>
    </includes>
 </artifactSet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜