1

I am trying to create a project through maven in which I can use Scala and Java both language. And I am trying to call Scala method in a java class. How can I achieve this?

I already have gone through this link Maven: mixing Java and Scala in one project

But I want to call scala method from java class.

POM:

    <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.simple.scala</groupId>
  <artifactId>hal-scala</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.11</version>
        </dependency>
    </dependencies>
  <build>
  <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.3.2</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>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                  <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                      <goal>add-source</goal>
                    </goals>
                    <configuration>
                      <sources>
                        <source>src/main/scala</source>
                      </sources>
                    </configuration>
                  </execution>
                </executions>
      </plugin>
        </plugins>
    </build>
</project>

Snapshot of the error:

SnapShot of error

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • 2
    SBT does this out of the box, have you considered using SBT instead, or are you restricted to using Maven? – Andrey Tyukin May 09 '18 at 16:33
  • https://www.scala-sbt.org/1.x/docs/sbt-by-example.html – Emiliano Martinez May 09 '18 at 16:37
  • Actually, my requirement is a create project which can be deployed in Apache Felix container. for this, I am creating a project through maven osgi bundle plugin. I am using a snippet in Scala which parse the XML and set all parsed element is Java class object, but this utility is running through java main method. Now java class & scala class in a default package, when I am segregating in different package then it's creating a problem. – user1788485 May 09 '18 at 18:50
  • Your configuration looks correct, although with some redundant elements. Have you tried running `mvn clean test` from the command line? – gjoranv May 09 '18 at 21:16
  • 1. What is the exact error (text) on eclipse ? 2. Are you able to build with maven on command line ? 3. How to build the eclipse configuration ? 4. Do you have scala-ide ? 5. You don't need build-helper-maven-plugin, the goal "add-source" of scala-maven-plugin is enougth – David Bernard May 11 '18 at 12:06

0 Answers0