Avijit
Avijit

Reputation: 1800

Execution of Scala and Maven project

I have created a simple maven project of Scala in eclipse's scala ide using below details -

Artifact Id - scala-archetype-simple

Group Id - net.alchim31.maven

After created the project I have modified the pom.xml file.

<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.test</groupId>
    <artifactId>scalapoc</artifactId>
    <version>0.0.1</version>
    <name>${project.artifactId}</name>
    <description>Test scala app</description>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.11.5</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
    </properties>

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

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-core_${scala.compat.version}</artifactId>
            <version>2.4.16</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-junit_${scala.compat.version}</artifactId>
            <version>2.4.16</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.compat.version}</artifactId>
            <version>2.2.4</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>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <!-- <arg>-make:transitive</arg> -->
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                            <archive>
                                <manifest>
                                    <mainClass>App.scala</mainClass>
                                </manifest>
                            </archive>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <!-- If you have classpath issue like NoDefClassError,... -->
                    <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I have also added manifest entry inside plugin tag.

<archive>
    <manifest>
        <mainClass>App.scala</mainClass>
    </manifest>
</archive>

I have only one App.scala file and one test file available in project. Build was successful. But when trying to execute the jar using java -jar <jar_name> getting below error -

no main manifest attribute, in scalapoc-0.0.1.jar

When trying to execute the command java -cp scalapoc-0.0.1.jar com.test.scalapoc.App.scala getting below error -

Error: Could not find or load main class com.test.scalapoc.App.scala

Please suggest what to be needed to execute the jar.

Upvotes: 1

Views: 492

Answers (1)

David Bernard
David Bernard

Reputation: 1640

try

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.test.scalapoc.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
  • .scala is for source only, source is not executable
  • archive tag is part of the configuration of a plugin

see https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make

UPDATE 2018-06-05:

To create a standalone jar (without need to defined a classpath with scala-library and other dependencies on command line), you should create a jar that included other classes/jar.

see:

Upvotes: 1

Related Questions