暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Maven 常见问题及解决方案

宇宙湾 2019-05-17
1521

如何使用其他语言编译出来的 Jar

Clojar

 如果需要在 Maven 中,添加新的 Repository,这里以 Clojar 为例,配置如下:

  1. <repositories>

  2. <repository>

  3. <id>clojars.org</id>

  4. <url>http://clojars.org/repo</url>

  5. </repository>

  6. </repositories>

本地缓存

 Maven 中出现 Repository 被缓存在本地,则可以进一步添加下面属性,以配置更新策略:

  1. <repository>

  2. <id>clojars.org</id>

  3. <url>http://clojars.org/repo</url>

  4. <releases>

  5. <enabled>true</enabled>

  6. <updatePolicy>always</updatePolicy>

  7. </releases>

  8. <snapshots>

  9. <enabled>true</enabled>

  10. <updatePolicy>always</updatePolicy>

  11. </snapshots>

  12. </repository>

下载问题

已 download,未 import

 如果发现 pom.xml
中的 依赖虽然在 .m2
中被下载了,但是没有被 import
到项目中  尝试在 Intellij Idea 的 setting 中 找到 Maven->IgnoredFiles
,看看对应的 pom.xml
有没有被勾选

如果仍然无法解决,也有可能是 Intellij Idea 自身的问题,可尝试通过 Just Restart 来解决

编译问题

JDK 版本不一致

描述
  1. Error:java: Compilation failed: internal java compiler error

解决

 调整 compiler 的级别,并在 pom.xml
中添加 build 标签,规定 compiler 的级别

  1. <build>

  2. <finalName>elastic-netflow-v5</finalName>


  3. <plugins>

  4. <plugin>

  5. <groupId>org.apache.maven.plugins</groupId>

  6. <artifactId>maven-compiler-plugin</artifactId>

  7. <version>2.3.2</version>

  8. <configuration>

  9. <source>1.7</source>

  10. <target>1.7</target>

  11. </configuration>

  12. </plugin>


  13. <plugin>

  14. <groupId>org.apache.maven.plugins</groupId>

  15. <artifactId>maven-release-plugin</artifactId>

  16. <version>2.4.1</version>

  17. </plugin>

  18. </plugins>

  19. </build>

Scala 版本不一致

描述
  1. [ERROR] error: error while loading <root>, Error accessing D:\.m2\repository\org\apache\curator\curator-client\2.4.0\curator-client-2.4.0.jar

  2. [ERROR] error: scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.

解决

 查看 pom 文件中指定的 scala 版本,切换到对应的版本,进行编译即可

存在没有下载好的依赖

描述
  1. Error creating properties files for forking; nested exception is java.io.IOException: No such file or directory

解决

 安装依赖

  1. $ mvn dependency::tree

 离线 build (offline)

  1. $ mvn install -o

本地 mvn clean install 到 .m2/repository 中的 jar 和 仓库中的不一致时

描述
  1. "Class not found 'xxxx' Empty test suite" in java unittest, after change the modules' names

解决
  1. Maven -> Reimport -> Generte Source and Update Folder

  2. Ctrl + F9 -> Make Project

打包存在脏程序

war:war
之前需要先执行 mvn clean install
。否则,会因为 profiles
的缘故,导致 war
包中 缺少文件

语法问题

Maven 中无法识别 ${project.version}

 需要将所有(除了 root / parent 模块)的 <version>1.0</version>
中的具体版本号(如 1.0.0
)全部替换成 "${project.version}"

插件问题

xxx module must not contain source root. the root already belongs to module xxx

解决
  1. Artifacts Setting -> Modules -> Sourc tab

  2. delete the fold by clicking on the X icon to the right of it

ExecutionException The forked VM terminated without properly saying goodbye

描述
  1. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project eagle-app-streamproxy: ExecutionException The forked VM terminated without properly saying goodbye. VM crash or System.exit called?

解决
  1. <plugin>

  2. <groupId>org.apache.maven.plugins</groupId>

  3. <artifactId>maven-surefire-plugin</artifactId>

  4. <version>${maven-surefire.version}</version>

  5. <configuration>

  6. <!-- 增加 argLine 参数以调整 JVM -->

  7. <argLine>-Xmx2048m -Xms1024m -XX:MaxPermSize=512m -XX:-UseGCOverheadLimit</argLine>

  8. <forkMode>always</forkMode>

  9. </configuration>

  10. </plugin>

详见:Eagle-RP-897

Fatal error compiling: 无效的目标发行版: 1.8

 需要在 IDE 的 ProjectStructure(Ctrl+Alt+Shift+S)
里设置 ProjectJDK
jdk1.8
,并在 Settings(Ctrl+Alt+S)
里设置 JavaCompiler
bytecode version
1.8

Assembly 插件报错 java.lang.StackOverflowError

解决
  1. # 调大默认的堆栈大小

  2. export MAVEN_OPTS=-Xss2m

补充
  1. -Xmsjvm 进程启动时分配的内存

  2. -Xmxjvm 进程运行过程中最大能分配到的内存

  3. -Xssjvm 进程中启动线程,为每个线程分配的内存大小

依赖问题

Maven 中央仓库找不到对应版本的依赖

如何将其他项目的 jar 安装到 maven 仓库中
利用命令安装
  1. $ mvn install:install-file -Dfile=<jar 包的位置> -DgroupId=<上面的 groupId> -DartifactId=<上面的 artifactId> -Dversion=<上面的 version> -Dpackaging=jar -DgeneratePom=true -DpomFile=<指定一个 pom.xml 添加进去>


  2. $ mvn install:install-file -Dfile=G:\ES\yuzhouwan_netflow-rest\lib\ite-esmanage-0.7.2-SNAPSHOT.jar -DgroupId=ite -DartifactId=esmanage -Dversion=0.7.2-SNAPSHOT -Dpackaging=jar -DgeneratePom=true -DpomFile=C:\yuzhouwan\Maven\pom.xml


  3. # -DgeneratePom 没有成功

  4. # 原因是 这个第三方的 jar 中,pom.xml 路径不对,必须手动 copy 出来,并用 -DpomFile 的方式导入

利用插件安装
  1. <!-- 注意:${project.basedir} 是当前模块的根目录,需要把依赖包,放对位置 -->

  2. <build>

  3. <pluginManagement>

  4. <plugins>

  5. <plugin>

  6. <groupId>org.apache.maven.plugins</groupId>

  7. <artifactId>maven-install-plugin</artifactId>

  8. <version>2.5.2</version>

  9. <executions>

  10. <execution>

  11. <id>install-external</id>

  12. <phase>clean</phase>

  13. <configuration>

  14. <file>${project.basedir}/lib/flume-hdfs-sink-${flume.version}.jar</file>

  15. <repositoryLayout>default</repositoryLayout>

  16. <groupId>org.apache.flume.flume-ng-sinks</groupId>

  17. <artifactId>flume-hdfs-sink</artifactId>

  18. <version>${flume.version}</version>

  19. <packaging>jar</packaging>

  20. <generatePom>true</generatePom>

  21. </configuration>

  22. <goals>

  23. <goal>install-file</goal>

  24. </goals>

  25. </execution>

  26. </executions>

  27. </plugin>

  28. </plugins>

  29. </pluginManagement>

  30. </build>

控制 Scope 范围

  1. # compile(编译范围)

  2.  compile 范围是默认的。编译范围依赖在所有的 classpath 中可用,同时它们也会被打包


  3. # provided(已提供范围)

  4.  provided 范围只有在当 JDK 或者一个容器已提供该依赖之后才使用。它们不是传递性的,也不会被打包。适用于在实现了公共 common 模块,其中有很多依赖,但是只有在其他依赖了 common 模块的子模块中,重新声明依赖,才会真正被打包到子模块中。这样,既可以保证 common 模块的正常编译,又可以减少子模块中的依赖包大小


  5. # runtime(运行时范围)

  6.  runtime 范围在运行和测试系统的时候需要,但在编译的时候不需要。例如,在编译的时候,如果只需要 JDBC API Jar,但只有在运行的时候才需要 JDBC 驱动的具体实现


  7. # test(测试范围)

  8.  test 范围在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。常用于 JUnit 的依赖


  9. # system(系统范围)

  10.  system 范围与 provided 类似,但是必须显式的提供一个对于本地系统中 Jar 文件的路径。这么做是为了允许基于本地对象编译(systemPath),而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven 也不会在仓库中去寻找它(不推荐使用)

引入某一个本身依赖树很庞大的 Dependency

  1. # 依赖列表

  2. $ mvn dependency:list


  3. # 依赖树(这里推荐直接用 Intellij Idea 中自带的 "Show Dependencies" 功能,可视化地展示依赖树 和 对应依赖冲突,并能够直接对 依赖树进行修剪)

  4. $ mvn dependency:tree


  5. # 依赖分析

  6. $ mvn dependency:analyze

  7. Unused declared dependencies 表示项目中未使用的,但显示声明的依赖

  8. Used undeclared dependencies 表示项目中使用到的,但是没有显示声明的依赖

Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError.

解决
  1. <spark.scala.version>2.11</spark.scala.version>

  2. <apache.kafka.version>0.9.0.1</apache.kafka.version>


  3. <dependency>

  4. <groupId>org.apache.kafka</groupId>

  5. <artifactId>kafka_${spark.scala.version}</artifactId>

  6. <version>${apache.kafka.version}</version>

  7. <exclusions>

  8. <exclusion>

  9. <groupId>org.slf4j</groupId>

  10. <artifactId>slf4j-log4j12</artifactId>

  11. </exclusion>

  12. <exclusion>

  13. <groupId>log4j</groupId>

  14. <artifactId>log4j</artifactId>

  15. </exclusion>

  16. </exclusions>

  17. </dependency>


文章转载自宇宙湾,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论