Maven 相关问题处理与plugins使用
问题1:
<strong> Error reading assembly descriptor: src/main/assembly/</strong>
处理办法:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<executions>
<execution>
<configuration>
<finalName>${project.version}</finalName>
</configuration>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
指定descriptor对应的地址
问题2:
<strong>Assembly: null is not configured correctly: Assembly ID must be present and non-empty.</strong>
解决办法:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/${project.version}</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
增加<id> 标签
问题3:
Malformed POM expected START_TAG or END_TAG
原因是 assembly.xml 格式混乱, 请format
解决办法:
format xml
问题4:
Maven项目出现Perhaps you are running on a JRE rather than a JDK?
解决办法:
1. On your Eclipse IDE, go into Window > Preferences > Java > Installed JREs > and check your installed JREs. You should choose JDK folder there.
选择JDK ,不要选择jre
问题5:
【java.lang.UnsupportedClassVersionError】
解决办法点击打开链接
一般是因为JDK 的问题
问题6:
the resource is not on the build path of a java project错误
解决办法
在eclipse中,使用mavenimport了一个工程,但是在对某一个类进行F3/F4/ctrl+alt+H操作的时候报错:“the resource is not on the build path of a java project”。
这种情况也常常发生在重新打开eclipse时。
原因:在该工程的构建路径中找不到该类。
解决方法:
1. 找到buildpath下面的src下的文件remove掉,然后重新加上去,此时会更新所有目录。
2. 关闭该类,重新打开即可
还有一个诡异的问题:
如果导入工程发现不是 maven 工程
工程结构如下:
解决办法:
删掉.project setting
然后工程右键
Java 要勾选
上面方案不好用 的话,用如下方式:
选中工程,右键
转换成Maven工程即可
关于pom.xml的配置详解
${project.build.sourceDirectory}:项目的主源码目录,默认为src/main/java/.
${project.build.testSourceDirectory}:项目的测试源码目录,默认为/src/test/java/.
${project.build.directory}:项目构建输出目录,默认为target/.
${project.build.outputDirectory}:项目主代码编译输出目录,默认为target/classes/.
${project.build.testOutputDirectory}:项目测试代码编译输出目录,默认为target/testclasses/.
${project.groupId}:项目的groupId.
${project.artifactId}:项目的artifactId.
${project.version}:项目的version,于${version}等价
${project.build.finalName}:项目打包输出文件的名称,默认为
${project.artifactId}${project.version}.
关于pom.xml配置描述
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 模型版本。maven2.0必须是这样写,现在是maven2唯一支持的版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.winner.trade,maven会将该项目打成的jar包放本地路径:/com/winner/trade -->
<groupId>com.winner.trade</groupId>
<!-- 本项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
<artifactId>trade-core</artifactId>
<!-- 本项目目前所处的版本号 -->
<version>1.0.0-SNAPSHOT</version>
<!-- 打包的机制,如pom,jar, maven-plugin, ejb, war, ear, rar, par,默认为jar -->
<packaging>jar</packaging>
<!-- 帮助定义构件输出的一些附属构件,附属构件与主构件对应,有时候需要加上classifier才能唯一的确定该构件 不能直接定义项目的classifer,因为附属构件不是项目直接默认生成的,而是由附加的插件帮助生成的 -->
<classifier>...</classifier>
<!-- 定义本项目的依赖关系 -->
<dependencies>
<!-- 每个dependency都对应这一个jar包 -->
<dependency>
<!--一般情况下,maven是通过groupId、artifactId、version这三个元素值(俗称坐标)来检索该构件, 然后引入你的工程。如果别人想引用你现在开发的这个项目(前提是已开发完毕并发布到了远程仓库),-->
<!--就需要在他的pom文件中新建一个dependency节点,将本项目的groupId、artifactId、version写入, maven就会把你上传的jar包下载到他的本地 -->
<groupId>com.winner.trade</groupId>
<artifactId>trade-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- maven认为,程序对外部的依赖会随着程序的所处阶段和应用场景而变化,所以maven中的依赖关系有作用域(scope)的限制。 -->
<!--scope包含如下的取值:compile(编译范围)、provided(已提供范围)、runtime(运行时范围)、test(测试范围)、system(系统范围) -->
<scope>test</scope>
<!-- 设置指依赖是否可选,默认为false,即子项目默认都继承:为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 -->
<optional>false</optional>
<!-- 屏蔽依赖关系。 比如项目中使用的libA依赖某个库的1.0版,libB依赖某个库的2.0版,现在想统一使用2.0版,就应该屏蔽掉对1.0版的依赖 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 为pom定义一些常量,在pom中的其它地方可以直接引用 使用方式 如下 :${file.encoding} -->
<properties>
<file.encoding>UTF-8</file.encoding>
<java.source.version>1.5</java.source.version>
<java.target.version>1.5</java.target.version>
</properties>
</project>
最后提供下 maven 常用的几个插件
<plugins>
<!-- 编译插件 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- 程序jar包存放路径 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>${project.build.directory}/${project.version}/lib</outputDirectory>
</configuration>
</plugin>
<!-- 依赖jar包存放路径 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.version}/lib</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<includeScope>compile</includeScope>
</configuration>
</plugin>
<!-- 配置、执行文件存放路径 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.version}</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>*.sh</include>
<include>*.bat</include>
<include>*.txt</include>
<include>config/**</include>
<!--兼容标准maven项目目录 -->
<include>resources/**</include>
</includes>
<!--兼容标准maven项目目录 -->
<excludes>
<exclude>resources/version.properties</exclude>
<exclude>resources/log4j2.xml</exclude>
</excludes>
</resource>
<!-- 兼容标准maven项目目录 <resource> <directory>${project.main.directory}</directory>
<includes> <include>config/**</include> </includes> </resource> <resource>
<directory>${project.main.directory}/resources</directory> <includes> <include>*.sh</include>
<include>*.bat</include> </includes> <filtering>true</filtering> </resource> -->
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- 组装zip包 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<executions>
<execution>
<configuration>
<finalName>${project.version}</finalName>
</configuration>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
</plugins>
请参考这位大师的总结:点击打开链接 https://www.cnblogs.com/heaven1025/p/6195825.html
还可以参考点击打开链接https://www.cnblogs.com/zhangxh20/p/6298062.html