遇到的问题:
mvn jar:jar
在使用maven打包的jar文件,再运行时,总出现classnotfound的错误。
首先是参照:
http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven
800多的高票答案。
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>edu.wit.fcl.gsontools.GsonFormat</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin
mvn clean compile assembly:single
再是参照:
http://m.oschina.net/blog/127531
http://panyongzheng.iteye.com/blog/1955763
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>edu.wit.fcl.gsontools.GsonFormat</mainClass> </manifest> </archive> </configuration> </plugin>
mvn clean package失败,gson还是没有打包进去
最后用maven生成ant,在自己修改build.xml才达到目地
先mvn ant:ant
再修改build.xml为
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== --> <!-- Ant build file (http://ant.apache.org/) for Ant 1.6.2 or above. --> <!-- ====================================================================== --> <project name="gsontools" default="create_run_jar" basedir="."> <!-- ====================================================================== --> <!-- Import maven-build.xml into the current project --> <!-- ====================================================================== --> <import file="maven-build.xml"/> <!-- ====================================================================== --> <!-- Help target --> <!-- ====================================================================== --> <target name="help"> <echo message="Please run: $ant -projecthelp"/> </target> <target name="create_run_jar"> <jar destfile="${maven.build.dir}/${maven.build.finalName}.jar" filesetmanifest="mergewithoutmain"> <manifest> <attribute name="Main-Class" value="edu.wit.fcl.gsontools.GsonFormat"/> <attribute name="Class-Path" value="."/> </manifest> <fileset dir="${maven.build.outputDir}"/> <zipfileset excludes="META-INF/*.SF" src="${maven.repo.local}/com/google/code/gson/gson/2.3/gson-2.3.jar"/> </jar> </target> </project>
因为时间有限,暂时这样解决了,应该是有更好方法,等待探究。
这个方式,存在手动改文件,比如 zipfileset 后面一段,需要复制
如果依赖太多,完全不可行。