[scala-tools] Questions about Scala + Maven in a mixed multimodule Java 1.6 + scala project

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Questions about Scala + Maven in a mixed multimodule Java 1.6 + scala project

Scott Carey
If this is the wrong place for these questions, please let me know
where else to go.

The goal:  to first use Scala in test scope to run unit tests against
Java code.  The Maven project is multimodule, with about 20 modues.
Some are web modules (type 'war').

I was able to get it to work but not in the way that was consistent
with the documentation at
at http://scala-tools.org/mvnsites/maven-scala-plugin/index.html .

The documentation there however is dated -- the timestamps on the web
pages are often old and refer to releases that are not the current one
(2.15.1).

First, the documentation there mentions adding scala-tools
repositories.  But the plugin is on maven central now.  It works fine
without adding those.

I want to confirm that what else I am doing is correct.

I added src/main/scala and src/test/scala.  I put my
"TestScalaTest.scala" class underneath src/test/scala .
I have added this to the project plugins:

      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>2.15.1</version>
        <executions>
          <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

It does not work if I do not specify executions for the compiler, are
there no default goals/phases for the plugin?

Next, I had to add the scala-library dependency.  This makes sense,
but isn't well documented -- http://scala-tools.org/mvnsites/maven-scala-plugin/usage.html
doesn't mention it.

    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.8.1</version>
      <scope>test</scope>
    </dependency>


Is there anything else the documentation is missing?

If I want to add non-test code, I can add a 'complie' goal to the
compile phase in the plugin, and change the scope of scala-library
from test to compile.  But what if I need to access scala classes from
Java?  What else would be needed?
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Questions about Scala + Maven in a mixed multimodule Java 1.6 + scala project

David Bernard-3
Hi,

On Sun, Jan 30, 2011 at 20:54, ScottC <[hidden email]> wrote:
> If this is the wrong place for these questions, please let me know
> where else to go.

You can also use ml
http://groups.google.com/group/maven-and-scala
(link in the upper right-corner in the site)

> The goal:  to first use Scala in test scope to run unit tests against
> Java code.  The Maven project is multimodule, with about 20 modues.
> Some are web modules (type 'war').
>
> I was able to get it to work but not in the way that was consistent
> with the documentation at
> at http://scala-tools.org/mvnsites/maven-scala-plugin/index.html .
>
> The documentation there however is dated -- the timestamps on the web
> pages are often old and refer to releases that are not the current one
> (2.15.1).

For a more up-to-date site (I can no longuer deploy on
scala-tools.org), but with the same info about the section you listed
  http://alchim31.free.fr/mvnsites/maven-scala-plugin/index.html

(I may have to update some part of the doc)

>
> First, the documentation there mentions adding scala-tools
> repositories.  But the plugin is on maven central now.  It works fine
> without adding those.

the plugin is deployed in scala-tools.org first, then replicated to central.
the snapshot version is only available on scala-tools.org repo.

> I want to confirm that what else I am doing is correct.
>
> I added src/main/scala and src/test/scala.  I put my
> "TestScalaTest.scala" class underneath src/test/scala .

Except if you use IDE, it's useless. the plugin should include thoses
directories implicitly.
And you should not include them for mixed java project.

> I have added this to the project plugins:
>
>      <plugin>
>        <groupId>org.scala-tools</groupId>
>        <artifactId>maven-scala-plugin</artifactId>
>        <version>2.15.1</version>
>        <executions>
>          <execution>
>            <id>test-compile</id>
>            <phase>test-compile</phase>
>            <goals>
>              <goal>testCompile</goal>
>            </goals>
>          </execution>
>        </executions>
>      </plugin>
>
> It does not work if I do not specify executions for the compiler, are
> there no default goals/phases for the plugin?

there no default goal, in your case default is testCompile but for the
majority it's compile.
But goal are default bind to the right phase, so from above you case
remove <phase> node (like in doc sample)

>
> Next, I had to add the scala-library dependency.  This makes sense,
> but isn't well documented -- http://scala-tools.org/mvnsites/maven-scala-plugin/usage.html
> doesn't mention it.
>
>    <dependency>
>      <groupId>org.scala-lang</groupId>
>      <artifactId>scala-library</artifactId>
>      <version>2.8.1</version>
>      <scope>test</scope>
>    </dependency>

I agree it's not clear (few words in section "Changing the scala version").
Note : pom packaging project can declare the plugin, but doesn't need
to declare the dependency.

>
> Is there anything else the documentation is missing?

May be lot of stuff (feel free to contribute via the wiki, patch or
the mailing-list)
I don't know ;-)


> If I want to add non-test code, I can add a 'complie' goal to the
> compile phase in the plugin,

from your example, remove <phase> node, add goal compile above testCompile.

> and change the scope of scala-library
> from test to compile.  But what if I need to access scala classes from
> Java?  What else would be needed?

There are 3 cases of scala/java projects (at least) :
* java call classes define in scala : bind scala compilation to phase
process-resources
* scala call classes define in java : default configuration (don't
bind scala compilation to an explicit phase)
* scala <-> java both way, full mixed : see doc, more complicated
because require scalac to run twice (before and after javac)

Cheers,

/davidB
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Questions about Scala + Maven in a mixed multimodule Java 1.6 + scala project

Scott Carey
Hi David,

On Jan 30, 2:14 pm, David Bernard <[hidden email]> wrote:

> For a more up-to-date site (I can no longuer deploy on
> scala-tools.org), but with the same info about the section you listed
>  http://alchim31.free.fr/mvnsites/maven-scala-plugin/index.html
>
> (I may have to update some part of the doc)
>
Thanks for the link!


>
> the plugin is deployed in scala-tools.org first, then replicated to central.
> the snapshot version is only available on scala-tools.org repo.

OK, If I need access to the snapshots for development I can add that.
Repositories can't be added to production pom.xml's easily (a common
policy).

>
> > I want to confirm that what else I am doing is correct.
>
> > I added src/main/scala and src/test/scala.  I put my
> > "TestScalaTest.scala" class underneath src/test/scala .
>
> Except if you use IDE, it's useless. the plugin should include thoses
> directories implicitly.
> And you should not include them for mixed java project.
>

For a mixed java/scala project, don't use src/main/scala or src/test/
scala for *.scala files?  Why?  I have so far left the *.java files in
their location and placed *.scala in its own folder hierarchy.
>
> there no default goal, in your case default is testCompile but for the
> majority it's compile.
> But goal are default bind to the right phase, so from above you case
> remove <phase> node (like in doc sample)
>

Thanks!
I'll remove the <phase>.

> I agree it's not clear (few words in section "Changing the scala version").
> Note : pom packaging project can declare the plugin, but doesn't need
> to declare the dependency.
>

Perhaps the simplest would be examples:

pom project.
scala only project
mix java/scala project

> > Is there anything else the documentation is missing?
>
> May be lot of stuff (feel free to contribute via the wiki, patch or
> the mailing-list)
> I don't know ;-)

Thanks, I didn't even see the link to the Wiki on the page before.
There is some very useful info in there.

> > and change the scope of scala-library
> > from test to compile.  But what if I need to access scala classes from
> > Java?  What else would be needed?
>
> There are 3 cases of scala/java projects (at least) :
> * java call classes define in scala : bind scala compilation to phase
> process-resources
> * scala call classes define in java : default configuration (don't
> bind scala compilation to an explicit phase)
> * scala <-> java both way, full mixed : see doc, more complicated
> because require scalac to run twice (before and after javac)
>
> Cheers,
>
> /davidB

Thank you!
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Questions about Scala + Maven in a mixed multimodule Java 1.6 + scala project

Scott Carey
As a summary for anyone who stumbles on this in the future:  I have
added the below to my pom to get it working well with Maven 3.0.1,
Scala 2.8.1, and the 2.15.1 maven-scala-plugin.  This is for running
Scala unit tests against Java code.  Some changes and additions are
necessary for non-test scope scala code.   See
http://groups.google.com/group/maven-and-scala/browse_thread/thread/40c99bf93ac09e94
as an example.
Future versions of the plugin should be faster and may not require
some of the config below. Some of the below will only work for Scala
2.8.0 +.  ≈

    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.8.1</version>
      <scope>test</scope>
    </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
       <groupId>org.scala-tools</groupId>
       <artifactId>maven-scala-plugin</artifactId>
      <version>2.15.1</version>
       <executions>
         <execution>
           <id>test-compile</id>
           <goals>
             <goal>testCompile</goal>
           </goals>
           <configuration>
             <args>
               <arg>-make:transitivenocp</arg>
               <arg>-dependencyfile</arg>
               <arg>$
{project.build.directory}/.scala_test_dependencies</arg>
             </args>
           </configuration>
         </execution>
       </executions>
     </plugin>