如何使用其他语言编译出来的 Jar
Clojar
如果需要在 Maven 中,添加新的 Repository,这里以 Clojar 为例,配置如下:
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
本地缓存
Maven 中出现 Repository 被缓存在本地,则可以进一步添加下面属性,以配置更新策略:
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
下载问题
已 download,未 import
如果发现 pom.xml
中的 依赖虽然在 .m2
中被下载了,但是没有被 import
到项目中
尝试在 Intellij Idea 的 setting 中 找到 Maven->IgnoredFiles
,看看对应的 pom.xml
有没有被勾选
如果仍然无法解决,也有可能是 Intellij Idea 自身的问题,可尝试通过 Just Restart 来解决
编译问题
JDK 版本不一致
描述
Error:java: Compilation failed: internal java compiler error
解决
调整 compiler 的级别,并在 pom.xml
中添加 build 标签,规定 compiler 的级别
<build>
<finalName>elastic-netflow-v5</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.1</version>
</plugin>
</plugins>
</build>
Scala 版本不一致
描述
[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
[ERROR] error: scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.
解决
查看 pom 文件中指定的 scala 版本,切换到对应的版本,进行编译即可
存在没有下载好的依赖
描述
Error creating properties files for forking; nested exception is java.io.IOException: No such file or directory
解决
安装依赖
$ mvn dependency::tree
离线 build (offline)
$ mvn install -o
本地 mvn clean install 到 .m2/repository 中的 jar 和 仓库中的不一致时
描述
"Class not found 'xxxx' Empty test suite" in java unittest, after change the modules' names
解决
Maven -> Reimport -> Generte Source and Update Folder
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
解决
Artifacts Setting -> Modules -> Sourc tab
delete the fold by clicking on the X icon to the right of it
ExecutionException The forked VM terminated without properly saying goodbye
描述
[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?
解决
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire.version}</version>
<configuration>
<!-- 增加 argLine 参数以调整 JVM -->
<argLine>-Xmx2048m -Xms1024m -XX:MaxPermSize=512m -XX:-UseGCOverheadLimit</argLine>
<forkMode>always</forkMode>
</configuration>
</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
解决
# 调大默认的堆栈大小
export MAVEN_OPTS=-Xss2m
补充
-Xms:jvm 进程启动时分配的内存
-Xmx:jvm 进程运行过程中最大能分配到的内存
-Xss:jvm 进程中启动线程,为每个线程分配的内存大小
依赖问题
Maven 中央仓库找不到对应版本的依赖
如何将其他项目的 jar 安装到 maven 仓库中
利用命令安装
$ mvn install:install-file -Dfile=<jar 包的位置> -DgroupId=<上面的 groupId> -DartifactId=<上面的 artifactId> -Dversion=<上面的 version> -Dpackaging=jar -DgeneratePom=true -DpomFile=<指定一个 pom.xml 添加进去>
$ 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
# -DgeneratePom 没有成功
# 原因是 这个第三方的 jar 中,pom.xml 路径不对,必须手动 copy 出来,并用 -DpomFile 的方式导入
利用插件安装
<!-- 注意:${project.basedir} 是当前模块的根目录,需要把依赖包,放对位置 -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${project.basedir}/lib/flume-hdfs-sink-${flume.version}.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.apache.flume.flume-ng-sinks</groupId>
<artifactId>flume-hdfs-sink</artifactId>
<version>${flume.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
控制 Scope 范围
# compile(编译范围)
compile 范围是默认的。编译范围依赖在所有的 classpath 中可用,同时它们也会被打包
# provided(已提供范围)
provided 范围只有在当 JDK 或者一个容器已提供该依赖之后才使用。它们不是传递性的,也不会被打包。适用于在实现了公共 common 模块,其中有很多依赖,但是只有在其他依赖了 common 模块的子模块中,重新声明依赖,才会真正被打包到子模块中。这样,既可以保证 common 模块的正常编译,又可以减少子模块中的依赖包大小
# runtime(运行时范围)
runtime 范围在运行和测试系统的时候需要,但在编译的时候不需要。例如,在编译的时候,如果只需要 JDBC API Jar,但只有在运行的时候才需要 JDBC 驱动的具体实现
# test(测试范围)
test 范围在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。常用于 JUnit 的依赖
# system(系统范围)
system 范围与 provided 类似,但是必须显式的提供一个对于本地系统中 Jar 文件的路径。这么做是为了允许基于本地对象编译(systemPath),而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven 也不会在仓库中去寻找它(不推荐使用)
引入某一个本身依赖树很庞大的 Dependency
# 依赖列表
$ mvn dependency:list
# 依赖树(这里推荐直接用 Intellij Idea 中自带的 "Show Dependencies" 功能,可视化地展示依赖树 和 对应依赖冲突,并能够直接对 依赖树进行修剪)
$ mvn dependency:tree
# 依赖分析
$ mvn dependency:analyze
Unused declared dependencies 表示项目中未使用的,但显示声明的依赖
Used undeclared dependencies 表示项目中使用到的,但是没有显示声明的依赖
Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError.
解决
<spark.scala.version>2.11</spark.scala.version>
<apache.kafka.version>0.9.0.1</apache.kafka.version>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_${spark.scala.version}</artifactId>
<version>${apache.kafka.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>




