davidB / scala-maven-plugin

The scala-maven-plugin (previously maven-scala-plugin) is used for compiling/testing/running/documenting scala code in maven.
https://davidb.github.io/scala-maven-plugin/
The Unlicense
557 stars 151 forks source link

Does new Scala 3 Syntax work with this plugin? #795

Closed yadavan88 closed 1 month ago

yadavan88 commented 1 month ago

Hi, I am trying to implement scala-cli export option to Maven. However, I am facing an issue. Here is the Scala code (in src/main/scala directory):

object SampleMaven {
    @main
    def main: Unit = println("****************")
}

Here is my pom.xml:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>com.lihaoyi</groupId>
      <artifactId>fansi_3</artifactId>
      <version>0.5.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala3-library_3</artifactId>
      <version>3.4.2</version>

    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>4.9.1</version>
        <configuration>
          <recompileMode>incremental</recompileMode>
          <javacArgs> </javacArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

When I execute the goal mvn compile exec:java -Dexec.mainClass=SampleMaven it fails to execute. If I change the scala code to:

object SampleMaven {
    def main(args: Array[String]): Unit = println("****************")
}

It works.

One more thing, when I run it using mvn compile exec:java -Dexec.mainClass=SampleMaven sometimes it fails. but if I do mvn clean first then mvn compile and then execute the exec:java command, it passes.

I haven't worked with Maven for over 7 years now, so I am not sure what is wrong. It seems the main class is not discoverable with the new @main annotation usage.

Edit: Forgot to mention: I also tried with

mvn scala:run -DmainClass=SampleMaven but got the same error about main not found

Thanks in advance

Gedochao commented 1 month ago

Managed to get it working with the following setup:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>maven-scala-seed3</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>maven-scala-seed3</name>
  <description>A minimal Scala project using the Maven build tool.</description>
  <licenses>
    <license>
      <name>My License</name>
      <url>http://....</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>3.3.3</scala.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala3-library_3</artifactId>
      <version>${scala.version}</version>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala3-compiler_3</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>org.scalameta</groupId>
      <artifactId>munit_3</artifactId>
      <version>1.0.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>4.9.2</version>
        <configuration></configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args></args>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
// ./src/main/scala/Smth.scala
@main def main = println("Hello from Maven")

works fine with

mvn scala:run -DmainClass=main
yadavan88 commented 1 month ago

Thanks @Gedochao It was my mistake. I was trying with -DmainClass=SampleMaven instead of -DmainClass=main It works now. I will close this issue now.