Mixing Scala and Java on Wicket

2019-07-28 22:25发布

问题:

We use Wicket as our front end framework and until now our application is pure Java. However, I would like to start using a bit of Scala on some of our new classes. The old code would stay in Java.

After installing the maven-scala-plugin, scala-library, scalatest dependencies and downloading them I had to face the problem that I do not know how to mix Scala with Java in Wicket. Until now I have only seen tutorials about either pure Java or pure Scala projects in Wicket.

In the extended class of WebApplication you have something such as

public Class<? extends Page> getHomePage() {
    return HomePage.class;
}

and if you are using Scala you would have something such as

def getHomePage = classOf[HomePage]

If I have a Scala class called HomePageScala.scala how can I call it from the java code?

How can I create a BookmarkablePageLink using Java code calling a Scala class? e.g

add(new BookmarkablePageLink<HomePageScala>("homePageScala", HomePageScala.class));

Is this actually even possible or do I have to use either 100% java or 100% scala?

Thank you very much in advance!

回答1:

It's possible to make cross reference between Scala and Java code. If you use maven, try this config. Tested with Scala 2.8.0:

 <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.14.1</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>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
                <args>
                    <arg>-target:jvm-1.5</arg>
                    <!-- to support mix java/scala only -->
                    <arg>-make:transitivenocp</arg>
                    <arg>-dependencyfile</arg>
                    <arg>${project.build.directory}/.scala_dependencies</arg>
                </args>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <compilerVersion>1.6</compilerVersion>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>

I'm not sure, but think that without maven you should to try this scalac argument:

-make:transitivenocp


回答2:

I would separate the project into a multi-module build.

root
     -- scala  (jar)   
     -- java   (jar) <dependency><artifactId>scala</artifactId></dependency>
     -- webapp (war) <dependency><artifactId>java</artifactId></dependency>

That way the Java code can easily reference the Scala code. Both JARs would be included in the webapp.

The drawback: Java could reference Scala code, but not the other way around (or vice-versa).