鍍金池/ 問答/Java  網(wǎng)絡(luò)安全/ IDEA Java Groovy 混編(Maven)問題

IDEA Java Groovy 混編(Maven)問題

編譯環(huán)境:
Mac OS 10.13.2
jdk1.7.0_151
IDEA 2017.3 Ultimate Edition
maven 3.5.2

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lyl.study</groupId>
    <artifactId>grv</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <!-- 使用 groovy 編譯 -->
        <dependency>
            <scope>compile</scope>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.3.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- groovy compiler -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
                <version>3.1</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <!--
                    <fork>true</fork>
                    <verbose>false</verbose>
                    -->
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <!--
                    <showDeprecation>true</showDeprecation>
                    <verbose/>
                    -->
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.9.1-01</version>
                    </dependency>
                    <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.3.7-01</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

例子代碼:

clipboard.png

Fruit.java:

package com.lyl.study.grv;

public class Fruit {
    private float width;
    private float height;

    public float getVolume() {
        return width * height;
    }

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
}

Apple.groovy:

package com.lyl.study.grv

import groovy.transform.CompileStatic

@CompileStatic
class Apple {
    @Delegate Fruit fruit;

    Apple(Fruit fruit) {
        this.fruit = fruit
    }
}

Main.java:

package com.lyl.study.grv;

public class Main {
    public static void main(String[] args) {
        Fruit f = new Fruit();
        f.setWidth(10);
        f.setHeight(100);
        System.out.println(f.getVolume());

        Apple a = new Apple(f);
        System.out.println(a.getVolume());
    }
}

無論是使用 IDEA 的 build,還是使用 Maven 的 compile,依然報(bào)如下錯(cuò)誤:

clipboard.png

但是反編譯出來的 target 文件夾中的 Apple.class 發(fā)現(xiàn)是有 getVolume 這個(gè)方法的,但是 Main.java 文件沒編譯出來。理論上在這個(gè)基礎(chǔ)上再編譯一次 Main.java 是會(huì)成功的。

clipboard.png

但是如果先使用 IDEA 的 build 方式(報(bào)錯(cuò)也不管),在 build 生成的 target 基礎(chǔ)上,再調(diào)用 maven 的 compile 居然編譯成功了?。。???

clipboard.png

個(gè)人分析:Maven 的 compile 指令的優(yōu)化原理是會(huì)基于原先 target 文件夾下的 maven-status 文件夾記錄的狀態(tài)信息只對(duì)一些改變了的源文件進(jìn)行編譯。剛好 IDEA 的 build 在 target 中沒生成 "maven-status" 文件夾,maven 的 compile
指令將會(huì)自動(dòng)找 class 文件創(chuàng)建時(shí)間比 java 文件創(chuàng)建時(shí)間要早的(源碼修改過);或?qū)ふ乙恍]被編譯的 java 文件(這里剛好就是缺了 Main.java)進(jìn)行編譯。這樣就會(huì)在已生成的 class 文件基礎(chǔ)上,再進(jìn)行一次編譯,Main.java 就編譯成功了。。。。

目前我配置了一下 IDEA 的運(yùn)行流程,把兩種編譯結(jié)合在一起,才能運(yùn)行。

這里記錄了一下個(gè)人入坑與脫坑的經(jīng)歷,也希望哪位路過的大神能給出一個(gè)更好的處理方案。

回答
編輯回答
壞脾滊

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>mixedJavaAndGroovy</artifactId>
<version>0.01-SNAPSHOT</version>


<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.targetEncoding>UTF-8</project.build.targetEncoding>
</properties>
<repositories>
    <repository>
        <id>Sonatype</id>
        <name>Sonatype Repository</name>
        <url>http://repository.sonatype.org/content/groups/public/</url>
        <layout>default</layout>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>artima</id>
        <name>Artima Maven Repository</name>
        <url>http://repo.artima.com/releases</url>
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.4.14</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.1-groovy-2.4</version>
        <scope>test</scope>
    </dependency>

</dependencies>


<build>
    <plugins>

        <!-- test -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/groovy</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <stubsOutputDirectory>${java.io.tmpdir}/${project.groupId}.${project.artifactId}/stubs</stubsOutputDirectory>
                <testStubsOutputDirectory>${java.io.tmpdir}/${project.groupId}.${project.artifactId}/test-stubs</testStubsOutputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>addSources</goal>
                        <goal>addTestSources</goal>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>generateTestStubs</goal>
                        <goal>compileTests</goal>
                        <goal>removeStubs</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>${java.io.tmpdir}/${project.groupId}.${project.artifactId}</directory>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
            <version>3.7.0</version>
        </plugin>

    </plugins>
    <pluginManagement>
        <plugins>
            <!--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.3.1,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>testCompile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        com.theoryinpractise
                                    </groupId>
                                    <artifactId>
                                        clojure-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.8.1,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>testCompile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.gmavenplus
                                    </groupId>
                                    <artifactId>
                                        gmavenplus-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.6.0,)
                                    </versionRange>
                                    <goals>
                                        <goal>addSources</goal>
                                        <goal>addTestSources</goal>
                                        <goal>generateStubs</goal>
                                        <goal>compile</goal>
                                        <goal>generateTestStubs</goal>
                                        <goal>compileTests</goal>
                                        <goal>removeStubs</goal>
                                        <goal>removeTestStubs</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

</project>

2017年11月8日 15:11
編輯回答
不將就

哥們我也在搞java 和 groovy 混編,看有什么問題可以交流下 我qq 是 750411463

2018年7月17日 23:28