11

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

我制作的 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>
        <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 而不是给出的 jvm maven-compiler-plugin<executable>/home/reyman/Logiciels/jdk1.7.0_80/bin/javac</executable>

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

4

4 回答 4

6

这对我有用:

<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>
于 2016-02-19T10:31:45.723 回答
4

使用这样的东西:

<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>
于 2016-02-19T10:44:40.617 回答
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'
于 2016-02-22T11:03:25.597 回答
2

这对我有用,但我必须允许 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>
于 2016-02-24T04:23:16.063 回答