Why does Travis CI think my code is Java 1.3, and how to fix it?

前提是你 提交于 2019-12-10 22:37:13

问题


I'm trying out Travis CI with a Java project (pretty standard Maven, Spring setup).

Based on Travis docs, this should suffice in the .travis.yml file:

language: java

Travis should notice the project's pom.xml and run mvn test by default.

However, the Travis build fails, giving me:

error: static import declarations are not supported in -source 1.3

My sources are Java 1.6. How and where should I tell that to Travis? The Java project docs don't mention -source option at all. (Also, 1.3 is a bit strange default, isn't it?)


回答1:


Got it working by customising install and script in .travis.yml like this:

language: java
install: mvn install -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6 -DskipTests=true
script: mvn test -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6

...as suggested in this answer.

Anyway, I don't know where exactly the Java 1.3 default comes from, but IMHO that should be fixed. Travis documentation talks of "reasonably good defaults", but in this case that doesn't seem to be true.

Another option would be to add a lengthy piece of Maven XML to explicitly specify the source & target levels. Well, personally I'm opposed to the idea of having to do that, just because a CI environment has silly defaults. (Isn't the whole point with Maven to avoid explicitly specifying stuff by using reasonable conventions?) With my current pom.xml everything works fine locally (e.g. mvn test) as well as when deploying on Heroku.

Update (2015, Java 8)

Just wanted to add that as of 2015, with a Java 8 codebase, you no longer need such customisations in Maven or Travis config. You get away with the following:

In pom.xml, under top-level <project>:

<properties>
    <java.version>1.8</java.version>
</properties>

And in .travis.yml:

language: java
jdk: oraclejdk8 

Which is nice. Note that JDK 8 must be specified, even when pom.xml sets Java version to 1.8. Travis otherwise defaults to JDK 7.




回答2:


This is because you do not explicit set the source and target levels in your maven-compiler-plugin configuration in your pom.xml.

Older versions of Maven then use the javac default which is Java 1.3 in OpenJDK (as opposed to 1.5 in Oracle Java).




回答3:


This problem is certainly related to a Bug in Maven3 Ubuntu package, which is currently installed on Travis worker machines.

Good News: In next Travis CI build environment, Maven 3.1.1 will be installed with Apache tarball release. This update is planned to be deployed in November 2013...



来源:https://stackoverflow.com/questions/19158720/why-does-travis-ci-think-my-code-is-java-1-3-and-how-to-fix-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!