IDEA Java Groovy 混编(Maven)问题

发布于 2022-09-06 20:24:30 字数 5323 浏览 22 评论 0

编译环境:
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,依然报如下错误:

clipboard.png

但是反编译出来的 target 文件夹中的 Apple.class 发现是有 getVolume 这个方法的,但是 Main.java 文件没编译出来。理论上在这个基础上再编译一次 Main.java 是会成功的。

clipboard.png

但是如果先使用 IDEA 的 build 方式(报错也不管),在 build 生成的 target 基础上,再调用 maven 的 compile 居然编译成功了!!???

clipboard.png

个人分析:Maven 的 compile 指令的优化原理是会基于原先 target 文件夹下的 maven-status 文件夹记录的状态信息只对一些改变了的源文件进行编译。刚好 IDEA 的 build 在 target 中没生成 "maven-status" 文件夹,maven 的 compile
指令将会自动找 class 文件创建时间比 java 文件创建时间要早的(源码修改过);或寻找一些没被编译的 java 文件(这里刚好就是缺了 Main.java)进行编译。这样就会在已生成的 class 文件基础上,再进行一次编译,Main.java 就编译成功了。。。。

目前我配置了一下 IDEA 的运行流程,把两种编译结合在一起,才能运行。

这里记录了一下个人入坑与脱坑的经历,也希望哪位路过的大神能给出一个更好的处理方案。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2022-09-13 20:24:30

哥们我也在搞java 和 groovy 混编,看有什么问题可以交流下 我qq 是 750411463

心不设防 2022-09-13 20:24:30

<?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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文