执行maven .war

时间:2017-04-16 19:52:05

标签: java eclipse hibernate maven war

我在使用maven为我的程序创建可执行战争时遇到了麻烦。

当前的问题是,当我在Eclipse中运行程序时,一切正常,但是一旦构建.war它就不起作用 - 它会抛出NoClassDefFoundError:/ org / Hibernate / SessionFactory。

因为这是main方法调用的第一个类,我怀疑它不是hibernate的问题,而是war的classpath,但我不明白为什么。这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<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>
	<groupId>pipe</groupId>
	<artifactId>pipe</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
		<!-- TODO project name  -->
	<name>pipe</name>
	<description></description>
	
	<!--
		<organization>
			<name>company name</name>
			<url>company url</url>
		</organization>
	-->
	<licenses>
		<license>
			<name>The Apache Software License, Version 2.0</name>
			<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
			<distribution>repo</distribution>
		</license>
	</licenses>
	<properties>
		<wicket.version>7.4.0</wicket.version>
		<jetty9.version>9.2.13.v20150730</jetty9.version>
		<log4j.version>2.5</log4j.version>
		<junit.version>4.12</junit.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- allowed values: R7, 1.0, 1.5, 2.0 or none -->
		<wtp.version>none</wtp.version>
	</properties>
	<dependencies>
		<!--  WICKET DEPENDENCIES -->
		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket-core</artifactId>
			<version>${wicket.version}</version>
		</dependency>
		
		<!-- OPTIONAL DEPENDENCY
		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket-extensions</artifactId>
			<version>${wicket.version}</version>
		</dependency>
		-->
<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-editable-grid</artifactId>
    <version>7.6.0</version>
</dependency>
<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-inmethod-grid</artifactId>
    <version>7.6.0</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.wicket/wicket-datetime -->
<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket-datetime</artifactId>
    <version>7.6.0</version>
</dependency>

  <!-- https://mvnrepository.com/artifact/org.apache.wicket/wicket-auth-roles -->
<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket-auth-roles</artifactId>
    <version>7.6.0</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.wicketstuff/wicketstuff-minis -->
<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-minis</artifactId>
    <version>7.6.0</version>
</dependency>

<dependency>
    <groupId>org.codehaus.izpack</groupId>
    <artifactId>izpack-maven-plugin</artifactId>
    <version>5.1.0</version>
</dependency>

<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>inmethod-grid</artifactId>
    <version>1.4.14</version>
</dependency>

		<!-- LOGGING DEPENDENCIES - LOG4J -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		

		<!--  JUNIT DEPENDENCY FOR TESTING -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<!--  JETTY DEPENDENCIES FOR TESTING  -->
		<dependency>
			<groupId>org.eclipse.jetty.aggregate</groupId>
			<artifactId>jetty-all</artifactId>
			<version>${jetty9.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<resources>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/java</directory>
				<includes>
					<include>**</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<filtering>false</filtering>
				<directory>src/test/resources</directory>
			</testResource>
			<testResource>
				<filtering>false</filtering>
				<directory>src/test/java</directory>
				<includes>
					<include>**</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</testResource>
		</testResources>
		<plugins>
			<plugin>
				<inherited>true</inherited>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>${jetty9.version}</version>
				<configuration>
					<systemProperties>
						<systemProperty>
							<name>maven.project.build.directory.test-classes</name>
							<value>${project.build.directory}/test-classes</value>
						</systemProperty>
					</systemProperties>
					<jettyXml>${project.basedir}/src/test/jetty/jetty.xml,${project.basedir}/src/test/jetty/jetty-ssl.xml,${project.basedir}/src/test/jetty/jetty-http.xml,${project.basedir}/src/test/jetty/jetty-https.xml</jettyXml>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<downloadSources>true</downloadSources>
					<wtpversion>${wtp.version}</wtpversion>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
					<archiveClasses>true</archiveClasses>
					<archive>
          <manifest>
            <mainClass>Main</mainClass>
          </manifest>
        </archive>
				</configuration>
			</plugin>
			   <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>main-class-placement</id>
                        <phase>prepare-package</phase>
                        <configuration>
                            <tasks>
                                <move todir="${project.build.directory}/${project.artifactId}-${project.version}/">
                                    <fileset dir="${project.build.directory}/classes/">
                                        <include name="Main.class" />
                                    </fileset>
                                </move>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>jetty-classpath</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>org.eclipse.jetty,javax.servlet</includeGroupIds>
                            <excludeArtifactIds>jsp-api,jstl</excludeArtifactIds>
                            <excludes>META-INF/ECLIPSEF.*</excludes>
                            <outputDirectory>
                                ${project.build.directory}/${project.artifactId}-${project.version}
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>Apache Nexus</id>
			<url>https://repository.apache.org/content/repositories/snapshots/</url>
			<releases>
				<enabled>false</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
	</project>

所有依赖项也位于Eclipse中的构建类路径中。 我试图用命令java -jar pipe-1.0.war来运行它。

感谢任何帮助

-edit -

确切错误:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/SessionFactory
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.hibernate.SessionFactory
        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)
        ... 7 more

2 个答案:

答案 0 :(得分:0)

如果您反编译jar并检查Manifest文件,您可能会看到使用main方法的类未正确声明。

因此,您可以尝试将此插件添加到您的pom xml文件中:

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>yourpackage.Mainclass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

答案 1 :(得分:-1)

Hibernate 5.2及更高版本至少需要Java 1.8,而你使用的是1.7:

<configuration>
    <source>1.7</source>
    <target>1.7</target>
    <encoding>UTF-8</encoding>
    <showWarnings>true</showWarnings>
    <showDeprecation>true</showDeprecation>
</configuration>