Dan*_*nDC 123 java settings maven-2 pom.xml
我正在尝试构建一个我修改过的Hudson插件,它需要jdk1.6.这很好,但我不知道如何告诉maven不同的jdk在哪里.我在互联网上发现很少提及但它们似乎并不适用于我.有人建议添加一些配置,.m2/settings.xml
但我没有settings.xml
.另外,我不想对所有maven版本使用1.6.
我mvn
在cygwin中使用的一个问题是,如果这很重要的话.看来我应该能够在项目pom文件中制作规范,但是现有的pom非常简单.
那么底线是,有没有办法为maven的单个调用指定一个jdk?
Pas*_*ent 125
那么底线是,有没有办法为maven的单个调用指定一个jdk?
暂时改变你的JAVA_HOME
.
Cer*_*ber 76
似乎maven现在提供了一个解决方案:使用不同的JDK编译源代码
让我们说你的JAVA_HOME
JDK7(它将运行maven进程)
你的pom.xml
可能是:
<build>
<plugins>
<!-- we want JDK 1.6 source and binary compatiblility -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- ... -->
<!-- we want sources to be processed by a specific 1.6 javac -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_6_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
如果您的开发人员只是添加(并自定义)以下行settings.xml
,那么您的pom将是独立于平台的:
<settings>
[...]
<profiles>
[...]
<profile>
<id>compiler</id>
<properties>
<JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
<JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>compiler</activeProfile>
</activeProfiles>
</settings>
Run Code Online (Sandbox Code Playgroud)
Jin*_*won 31
compile:compile有一个用户属性,允许你指定一个路径javac
.
需要注意的是,当该用户属性只能fork
是true
这是false
默认.
$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile
Run Code Online (Sandbox Code Playgroud)
如果值包含空格,则可能必须对该值进行双引号.
> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile
Run Code Online (Sandbox Code Playgroud)
另请参阅Maven自定义属性优先级.
Dev*_*der 24
正如你所说的"另外,我不想为所有maven构建使用1.6."....所以我会说更好你的pom文件并指定使用哪个jdk版本.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
它将确保您的特定项目使用该版本的jdk.
fen*_*iix 14
我说你设置JAVA_HOME就像Pascal说的那样:在cygwin中如果你使用bash作为你的shell应该是"export JAVA_HOME =/cygdrive/c/pathtothejdk"并且它也永远不会因为导出PATH而将java bin目录导出到PATH = $ {JAVA_HOME}/bin中:$ {PATH}"
并添加maven-enforce-plugin以确保使用正确的jdk.这对你的pom来说是一个很好的做法.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.6</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
http://maven.apache.org/plugins/maven-enforcer-plugin/usage.html
小智 11
我知道它是一个旧线程.但是我在Maven for Java 8编译器源代码中遇到了类似的问题.我想通过本文中提到的快速解决方案来解决这个问题,我认为我可以把它放在这里,也许可以帮助其他人:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)
Rit*_*ito 11
如果您已经安装了 Java brew
,Mac
那么您很可能会在这里找到您的 Java 主目录:
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Run Code Online (Sandbox Code Playgroud)
现在下一步是找到Java Home
maven 指向的目录。要找到它,请输入以下命令:
mvn -version
我们在这里感兴趣的字段是:
Java version
和runtime
。
Maven 当前指向Java 13
. 另外,在keyruntime下可以看到Java Home路径,即:
/usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
要更改maven的Java版本,我们需要Java 8
在JAVA_HOME
env变量中添加home路径。
为此,我们需要
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
在终端中运行命令:
现在,如果我们检查 maven 版本,我们可以看到它现在指向 Java 8。
这样做的问题是,如果您在新终端中再次检查 Maven 版本,您会发现它指向 Java 13。为避免这种情况,我建议JAVA_HOME
在~/.profile
文件中添加该变量。
这样,无论何时您的终端正在加载,它都会默认使用您在 JAVA_HOME 中定义的值。这是您需要在~/.profile
文件中添加的行:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
您可以打开一个新终端并检查Maven版本,(mvn -version
)您会发现它这次指向Java 8。
您还可以在主目录中的文件中设置 Maven 的 JDK ~/.mavenrc
:
JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home'
Run Code Online (Sandbox Code Playgroud)
mvn 脚本将检查此环境变量并在存在时使用:
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
Run Code Online (Sandbox Code Playgroud)
https://github.com/CodeFX-org/mvn-java-9/tree/master/mavenrc
归档时间: |
|
查看次数: |
224701 次 |
最近记录: |