最近开发一个swing程序,之前用的普通java 项目创建的,打包运行没问题,后来东西多了,jar管理不方便,于是改成maven项目,spring+mybatis的项目,项目里边大部分用的pom引入的jar,但是有几个 jar 是自己从网上找的,前几天要上线发布,我就按平时操作打包,
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.coffee.bee.Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
打包时提示找不到符号包 啥的 这里没有截图 ,网上找个提示 大概就是这个意思
... [ERROR] [ERROR] \ideaProjects\xxx\ProjectService.java:[201,73] 错误: 找不到符号 [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException Process finished with exit code 1
这是网上找的 提示 大概的意思就是找不到方法 就是jar没有引入,然后网上找到一些方法
方法一
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>lib/</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins>
这个试了不行,
方法二
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
红色部分弄成自己引入jar的位置,然后我打包,打包可以成功, 但是 但是 但是运行不了 使用cmd 运行提示 找不到 自己引入jar 的那个方法,然后 找了许多博客 ,试了很多方法,终究是不行,可能是自己的问题,
最后总结了一下 解决办法 就俩种 ,第一种 把自己本地jar 弄到maven仓库 ,大概找了几个命令
mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\taobao-sdk-java-1455552377940-20160607.jar -DgroupId=com.taobao -DartifactId=taobao-sdk-java -Dversion=1455552377940-20160607 -Dpackaging=jar
要是自己引入一俩个 还好说 引入的多了 那岂不是很麻烦 于是果断放弃这种的方式,
第二种就是 用maven 引入jar的方式 按照正常的走 去 http://mvnrepository.com/ 找 大部分jar都能找见,最后发现又绕回来了 , 这里推荐用 maven-shade-plugin 插件打包,不要问我为什么,因为我也不知道 ,从坑里爬出来的,实践出来的,
然后打包 ,测试能正常进入软件 但是登录的时候连接数据库的时候 发现 spring 初始化失败,大概就是找不到 xml
解决方案就是
<transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.peak.App</mainClass> </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> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.tooling</resource> </transformer> </transformers>
就是指定一下spring xml的位置 ,打包就ok,再次运行的时候又发现 连接不上数据库,提示xml 不存在,我用解压工具进去 看见确实没有xml,原因是我的xml放在 src/main.java 里边的mapper下边, maven打包的时只打包 .jar 文件,解决办法也是俩种
解决办法:第一种方法:可以将xml放到 resources目录里面,这样做要改一下配置文件:把扫描xml的
把扫描包的路径改成相应的路径即可 。
第二种解决方法:在pom.xml中配置:在build标签中添加如下内容:
<resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!-- 是否替换资源中的属性--> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <!-- <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> --> </resource> </resources>
把非Java的文件配置到resource中,上面的配置配的是src/main/java下面的属性文件和xml文件,以及src/main/resources目录下的属性文件和xml文件
我用的是第一种方法 把xml放到 resource下自己建了一个xml文件, 最好打包执行成功 最后贴出 pom
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <finalName>my-spring-app</finalName> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>jar-with-dependencies</shadedClassifierName> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>${exec.mainClass}</mainClass> </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>
注意:这个方法可能不适合大家 ,我这里只是记录一下当时解决问题思路,也许对大家有用,