scala and java mixed development package

How to package the mixed development of scala and java?

First, create maven project and add dependency in pom.xml

<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>3.8.1</junit.version>
		<slf4j-api.version>1.6.4</slf4j-api.version>
		<commons-lang3>3.1</commons-lang3>
		<scala.version>2.11.8</scala.version>
		<maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version>
	</properties>


	<dependencies>
		<dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
		<!-- log API -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j-api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
		   <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>spark.example.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
		</plugins>
	</build>

Among them, mainClass is the main function of the last jar package, usually the scala class containing the main method. You can also not specify it.

mvn clean scala:compile package

After packaging, there will be. / lib subdirectory under the same jar directory, which contains scala-library-2.11.8.jar and other dependent packages.

If the dependent package is in a different directory, you can also use the command:

java -Djava.ext.dirs=c:\libs -jar my-snapshot.jar

To execute. Where - Djava.ext.dirs=c:\libs specifies that the path of the dependency package is c:\libs, and you can change to your own directory.

Tags: Big Data Maven Scala Java Apache

Posted on Sat, 09 May 2020 10:19:27 -0400 by SueHubert