简体   繁体   中英

scala maven plugin not packaging scala files into jar

I'm new to maven and currently try to assemble a scala project with it. Project structure:

dir
 |
 |--src/main/java
 |
 |--src/main/scala
 |
 |--pom.xml

I was kind of surprised that classes compiled from *.java end up in jar, but one compiled from *.scala do not. I added these plugins to pom.xml

<plugins>
    <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.1.3</version>
    </plugin>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>HelloWorld</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
</plugins>

I though maven-jar-plugin is responsible for assembling jar files. But for some reason, it does not add scala-compiled classes.

QUESTION: Who adds .class file into a final jar after executing mvn install ? How to add .class -files compiled with scala compiler?

mvn package will build your jar, however maven-compiler-plugin will only compile your java source files not your scala source files. Scala-maven-plugin can be used to compile both java and scala sources.

I wrote a blog post on this a while ago, that may help http://blog.rizvn.com/2016/04/scala-and-maven.html

You will need to tell maven about src/main/scala, since you are putting your scala code under src/main/java. This is done through the build section like so:

<build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
 ...
</build>

Can you try something like this, using "scala-maven-plugin" instead. Then execute maven goal : mvn clean package

<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
    <resource>
        <directory>${basedir}/src/test/resources</directory>
    </resource>
</resources>
<plugins>
    <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>${scala-maven-plugin.version}</version>
        <configuration>
            <sourceDir>${basedir}/src</sourceDir>
            <outputDir>${basedir}/target/classes</outputDir>
        </configuration>
        <executions>
            <execution>
                <id>scala-compile-first</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
 </plugins>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM