【问题标题】:pom configuration to force usage of jvm 7 with scala maven plugin使用 scala maven 插件强制使用 jvm 7 的 pom 配置
【发布时间】:2016-05-28 02:26:25
【问题描述】:

由于 scala 2.9.2 项目与 java 8 版本不兼容,我需要在我的 maven 项目中手动指定 jvm 使用情况。

我制作的 pom.xml,使用文档 here

<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.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.netlogo.extension</groupId>
    <packaging>jar</packaging>
    <artifactId>rungekuta</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${lib.org.scala-lang.scala.version}</version>
        </dependency>
        <dependency>
           <groupId>org.nlogo</groupId>
           <artifactId>netlogo</artifactId>
            <version>5.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <lib.org.scala-lang.scala.version>2.9.3</lib.org.scala-lang.scala.version>
        <maven.scala.version>${lib.org.scala-lang.scala.version}</maven.scala.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Extension-Name>rungekuta</Extension-Name>
                            <Class-Manager>org.netlogo.extension.rungeKuta.RungeKutaExtension</Class-Manager>
                            <NetLogo-Extension-API-Version>5.0</NetLogo-Extension-API-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <executable>/home/reyman/Logiciels/jdk1.7.0_80/bin/javac</executable>
                    <compilerVersion>1.3</compilerVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <name>${project.artifactId} ${project.version}</name>


    <repositories>
        <repository>
            <id>snapshots.scala-tools.org</id>
            <name>Scala snapshots repository</name>
            <url>http://scala-tools.org/repo-snapshots/</url>
        </repository>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala repository</name>
            <url>http://scala-tools.org/repo-releases/</url>
        </repository>
    </repositories>

</project>

我尝试这个没有成功,maven 继续使用我当前的 jvm 8 而不是maven-compiler-plugin 中给出的 jvm:&lt;executable&gt;/home/reyman/Logiciels/jdk1.7.0_80/bin/javac&lt;/executable&gt;

如何在我的混合 scala/java 源项目的mvn compile 期间强制使用 jvm 7?

【问题讨论】:

  • &lt;compilerVersion&gt;1.3&lt;/compilerVersion&gt; 怎么了?
  • 嗯,文档说是1.3还是1.5,不是很清楚……:maven.apache.org/plugins/maven-compiler-plugin/…
  • 这是一个例子。您可以尝试将其设置为1.77 吗?
  • 同样的问题,我用 1.7 和 7 测试:/ 似乎没有任何改变
  • 使用此代码:&lt;plugin&gt; &lt;groupId&gt;net.alchim31.maven&lt;/groupId&gt; &lt;artifactId&gt;scala-maven-plugin&lt;/artifactId&gt; &lt;version&gt;3.2.1&lt;/version&gt; &lt;configuration&gt; &lt;source&gt;${java.version}&lt;/source&gt; &lt;target&gt;${java.version}&lt;/target&gt; &lt;/configuration&gt; &lt;/plugin&gt;

标签: scala maven pom.xml maven-plugin scala-maven-plugin


【解决方案1】:

这对我有用:

<plugin>
    <inherited>true</inherited>
    <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>

【讨论】:

  • 你好,我试了没有成功,你能给我完整的&lt;build&gt;&lt;/build&gt;吗?
【解决方案2】:

使用这样的东西:

<plugin> 
 <groupId>net.alchim31.maven</groupId>
 <artifactId>scala-maven-plugin</artifactId> 
 <version>3.2.1</version>
 <configuration>
    <source>${java.version}</source>
    <target>${java.version}</target> 
 </configuration>
</plugin>

根据需要进行修改。

这是我的完整版本,不知道对你有没有帮助。 我把它贴在这里是因为它太长了,无法评论:

    <build>
    <finalName>KAIBICHI</finalName>
    <pluginManagement>
        <plugins>
          <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 您好,我尝试了但没有成功,您能给我完整的 吗?我需要 scala/java 编译。
  • 我没有在你的完整版本中看到 >scala-maven-plugin
【解决方案3】:

我在尝试将 minify-maven-plugin 1.7 与 java6 一起使用时遇到了同样的问题。它需要Java7。 问题是 Maven 需要使用正确的 JVM 运行才能使用插件,所以我找到的唯一解决方案是在运行 maven 之前导出 JAVA_HOME:

ORIGINAL_JAVA_HOME=$JAVA_HOME
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79
mvn jetty:run
export JAVA_HOME=$ORIGINAL_JAVA_HOME

如果你需要经常这样做,也许你可以在你的 bashrc 中添加一个别名

alias mvn7='ORIGINAL_JAVA_HOME=$JAVA_HOME; export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79; mvn jetty:run; export JAVA_HOME=$ORIGINAL_JAVA_HOME'

【讨论】:

【解决方案4】:

这对我有用,但我必须允许 Eclipse (Mars) 调整插件配置。我用一个空项目测试了我的 pom.xml,它似乎可以顺利构建。

我正在使用jenv 在 Eclipse 之外管理我的 Java 环境,当您安装了多个 Java 版本时,这会容易得多。

这是我正在使用的 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.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>org.netlogo.extension</groupId>
        <packaging>jar</packaging>
        <artifactId>rungekuta</artifactId>
        <version>1.0-SNAPSHOT</version>

        <dependencies>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>${lib.org.scala-lang.scala.version}</version>
            </dependency>
            <!--  
            <dependency>
               <groupId>org.nlogo</groupId>
               <artifactId>netlogo</artifactId>
                <version>5.2</version>
                <scope>provided</scope>
            </dependency>
            -->
        </dependencies>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <lib.org.scala-lang.scala.version>2.9.3</lib.org.scala-lang.scala.version>
            <maven.scala.version>${lib.org.scala-lang.scala.version}</maven.scala.version>
        </properties>

        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>net.alchim31.maven</groupId>
                        <artifactId>scala-maven-plugin</artifactId>
                        <version>3.2.1</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.5.1</version>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                    </plugin>
                    <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                    <plugin>
                        <groupId>org.eclipse.m2e</groupId>
                        <artifactId>lifecycle-mapping</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <lifecycleMappingMetadata>
                                <pluginExecutions>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>
                                                net.alchim31.maven
                                            </groupId>
                                            <artifactId>
                                                scala-maven-plugin
                                            </artifactId>
                                            <versionRange>
                                                [3.2.1,)
                                            </versionRange>
                                            <goals>
                                                <goal>testCompile</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore></ignore>
                                        </action>
                                    </pluginExecution>
                                </pluginExecutions>
                            </lifecycleMappingMetadata>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>add-source</goal>
                                <goal>compile</goal>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <archive>
                            <manifestEntries>
                                <Extension-Name>rungekuta</Extension-Name>
                                <Class-Manager>org.netlogo.extension.rungeKuta.RungeKutaExtension</Class-Manager>
                                <NetLogo-Extension-API-Version>5.0</NetLogo-Extension-API-Version>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>


        <name>${project.artifactId} ${project.version}</name>


        <repositories>
            <repository>
                <id>snapshots.scala-tools.org</id>
                <name>Scala snapshots repository</name>
                <url>http://scala-tools.org/repo-snapshots/</url>
            </repository>
            <repository>
                <id>scala-tools.org</id>
                <name>Scala repository</name>
                <url>http://scala-tools.org/repo-releases/</url>
            </repository>
        </repositories>

    </project>

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2011-12-09
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多