Scala和Java与默认eclipse中的Maven

时间:2023-01-20 16:58:58

Is it possible to use the default eclipse (Eclipse IDE for Java EE Developers or Eclipse IDE for Java Developers) to access scala source in a java class?

是否可以使用默认的eclipse(Eclipse IDE for Java EE Developers或Eclipse IDE for Java Developers)来访问java类中的scala源?

I have no problems to acces Java code in Scala and vise versa using the Scala-IDE but not in the default eclipse. I can compile the source using the following pom.xml and execute it. My source folder structure is src/main/java, src/main/scala, src/test/scala and src/test/java. Is this even possible or do I need the ScalaIDE? When I download the ScalaIDE-Plugin for eclipse and add the scala nature to the project it's working.

我没有问题在Scala中访问Java代码,反之亦然,使用Scala-IDE但不在默认的eclipse中。我可以使用以下pom.xml编译源代码并执行它。我的源文件夹结构是src / main / java,src / main / scala,src / test / scala和src / test / java。这是可能的还是我需要ScalaIDE?当我为eclipse下载ScalaIDE-Plugin并将scala特性添加到项目中时,它正在运行。

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.11.7</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
    </dependency>
</dependencies>

<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>2.0.2</version>
            </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>compile</goal>
                                        <goal>add-source</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>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
            <!--    <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>-->
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>test.JavaMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

2 个解决方案

#1


0  

Yes, you are right, we don't need SCALA IDE Separately, we can do it using ECLIPSE IDE Itself.

是的,你是对的,我们不需要SCALA IDE另外,我们可以使用ECLIPSE IDE本身。

http://davidb.github.io/scala-maven-plugin/example_java.html

#2


-1  

this question was asked before here

这个问题在此之前被问过

With maven multi-module/simple project ensure that you are using the sames scala library .jar version in your pom (=>classpath) both ur eclipse/plugins/ directory

使用maven多模块/简单项目确保你在你的pom(=> classpath)中使用相同的scala库.jar版本你的eclipse / plugins /目录

I hope that help you.

我希望能帮助你。

#1


0  

Yes, you are right, we don't need SCALA IDE Separately, we can do it using ECLIPSE IDE Itself.

是的,你是对的,我们不需要SCALA IDE另外,我们可以使用ECLIPSE IDE本身。

http://davidb.github.io/scala-maven-plugin/example_java.html

#2


-1  

this question was asked before here

这个问题在此之前被问过

With maven multi-module/simple project ensure that you are using the sames scala library .jar version in your pom (=>classpath) both ur eclipse/plugins/ directory

使用maven多模块/简单项目确保你在你的pom(=> classpath)中使用相同的scala库.jar版本你的eclipse / plugins /目录

I hope that help you.

我希望能帮助你。