Giter VIP home page Giter VIP logo

Comments (12)

slandelle avatar slandelle commented on September 6, 2024

Hi,

See https://github.com/davidB/scala-maven-plugin/blob/master/src/main/java/scala_maven/ScalaMojoSupport.java#L131

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

Many many thanks. That solved the issue I mentioned, but there is still one issue:

When I actually use Java 21's String templates, e.g. by running System.out.println(STR."=\{2+2}");, I get the following error at the beginning of the compile step

invalid escape character

, which makes sense from the point of view of previous Java versions, but not Java 21. Moreover, java.lang.StringTemplate.STR is correctly imported and I can freely use it as long I don't have \{ in my String which of course defeats the purpose of templates. Is there any solution for this one?

from scala-maven-plugin.

slandelle avatar slandelle commented on September 6, 2024

Please provide a reproducer as described here: https://stackoverflow.com/help/minimal-reproducible-example.

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

pom.xml:

<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>io.aghasemi</groupId>
  <artifactId>mvntest</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.13.12</scala.version>
    <scala.compat.version>2.13</scala.compat.version>
  </properties>

  <dependencies>
    

    <!-- https://mvnrepository.com/artifact/org.scalatest/scalatest -->
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_${scala.compat.version}</artifactId>
      <version>3.2.17</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.scalatestplus/junit-4-13 -->
    <dependency>
      <groupId>org.scalatestplus</groupId>
      <artifactId>junit-4-13_${scala.compat.version}</artifactId>
      <version>3.2.17.0</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>4.8.1</version>
        <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>
        <configuration>
          <javacArgs>--enable-preview</javacArgs>
          <jvmArgs>
            <jvmArg>--enable-preview</jvmArg>
          </jvmArgs>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>

        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
          <enablePreview>true</enablePreview>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.21.0</version>
        <configuration>
          <!-- Tests will be run with scalatest-maven-plugin instead -->
          <skipTests>true</skipTests>
          <argLine>--enable-preview</argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.scalatest</groupId>
        <artifactId>scalatest-maven-plugin</artifactId>
        <version>2.0.0</version>
        <configuration>
          <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
          <junitxml>.</junitxml>
          <filereports>TestSuiteReport.txt</filereports>
          <!-- Comma separated list of JUnit test class names to execute -->
          <jUnitClasses>io.aghasemi.Tests</jUnitClasses>
          <argLine>
            --enable-preview
          </argLine>
        </configuration>
        <executions>
          <execution>
            <id>test</id>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.6.0</version>

        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>

      </plugin>
    </plugins>
  </build>
</project>

and then io.aghasmi.Tests.java:

package io.aghasemi;

import static java.lang.StringTemplate.STR;

public class Tests {

    public static void main(String[] args) {
         System.out.println(STR."=\{2+2}");

        return;
    }
}

Temurin JDk 21 and Maven 3.9.4.

from scala-maven-plugin.

slandelle avatar slandelle commented on September 6, 2024

Could you please:

  • trim down your pom.xml to what's really necessary
  • push a maven project in a repo on GitHub

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

Could you please:

  • trim down your pom.xml to what's really necessary
  • push a maven project in a repo on GitHub

Here it is: https://github.com/aghasemi/mavan-scala-java21-test

Thanks

from scala-maven-plugin.

slandelle avatar slandelle commented on September 6, 2024

I have the exact same error with sbt.
It seems the Scala compiler doesn't support String templates yet (and I expect supporting an experimental Java feature to be very low priority).

Nothing we can do on our side. scala-maven-plugin will support automatically it once scalac supports it.

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

I see. Thanks. Quite a bit of a shame since the Scala compiler is actually OK with the preview features, except for this extension to escape characters it seems. Would not happen if \ was not chosen to use in templated strings :)

from scala-maven-plugin.

slandelle avatar slandelle commented on September 6, 2024

2 different things: being able to pass the javac option to enable features in preview and specifically being able to parse this new Java code. Might be related to the version of asm currently shipped.

Anyway, nothing we can do here. I recommend that you open an issue directly against Scala.

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

Thanks. I will. Just out of curiosity, why should the Scala compiler parse a Java source file?

from scala-maven-plugin.

slandelle avatar slandelle commented on September 6, 2024

So it can figure out the types and methods in Java code and compile the Scala code that might import them.

But it just parses Java sources, it doesn't compile them in bytecode, javac is used for that.
That's why you only need a JRE to compile Scala code, but you need a full JDK to compile mixed projects with Java code.

from scala-maven-plugin.

aghasemi avatar aghasemi commented on September 6, 2024

Of course! Many thanks.

from scala-maven-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.