Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My mixed Scala/Java Maven project doesn't compile

Tags:

I have a mixed Scala/Java project that doesn't compile well.

The problem arises when Java code is trying to call Scala code in the same package.

Of course, I have the standard layout:

  • src/main/java
  • src/main/scala
  • src/test/java
  • src/test/scala

I've looked at other similar Stackoverflow questions but this question is a little outdated. This other question doesn't help either.

I have also followed the scala-maven-plugin documentation page.

<build>     <pluginManagement>         <plugins>             <plugin>                 <groupId>net.alchim31.maven</groupId>                 <artifactId>scala-maven-plugin</artifactId>                 <version>3.1.6</version>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>2.0.2</version>             </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>             <executions>                 <execution>                     <phase>compile</phase>                     <goals>                         <goal>compile</goal>                     </goals>                 </execution>             </executions>         </plugin>     </plugins> </build> 

I have tried unsuccessfully to follow this blog post.

IDEA project with Scala plugin imported from the pom.xml can compile and run my project successfully.

What is the right way of doing it? Does the Java code gets compiled twice? First by the Scala plugin and the by the Java plugin?.

like image 996
david.perez Avatar asked Jun 27 '14 09:06

david.perez


People also ask

Does Maven work with scala?

If you're familiar with Maven, you can go ahead with the Scala Maven Plugin.

Is Maven compiler plugin necessary?

Maven Compiler Plugin might be the most important plugin in Maven. It is used to compile the sources of your project, which transform Java files ( *. java ) into class files ( *.

How do I create a Maven project in scala IDE?

Create a new Scala Maven projectSelect New -> Project -> Other and then select Maven Project. On the next window, search for scala-archetype. Make sure you select the one in group net.


1 Answers

Here is a working example of 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>stackoverflow</groupId> <artifactId>q24448582</artifactId> <version>1.0-SNAPSHOT</version>  <properties>     <scala.version>2.10.3</scala.version> </properties>  <dependencies>     <dependency>         <groupId>org.scala-lang</groupId>         <artifactId>scala-library</artifactId>         <version>${scala.version}</version>     </dependency>  </dependencies>  <build>     <plugins>         <plugin>             <groupId>org.scala-tools</groupId>             <artifactId>maven-scala-plugin</artifactId>             <version>2.15.2</version>             <executions>                  <execution>                     <id>compile</id>                     <goals>                         <goal>compile</goal>                     </goals>                     <phase>compile</phase>                 </execution>                 <execution>                     <id>test-compile</id>                     <goals>                         <goal>testCompile</goal>                     </goals>                     <phase>test-compile</phase>                 </execution>                 <execution>                     <phase>process-resources</phase>                     <goals>                         <goal>compile</goal>                     </goals>                 </execution>             </executions>         </plugin>     </plugins> </build>  </project> 
like image 140
maksim07 Avatar answered Oct 03 '22 22:10

maksim07