IDEA New Scala+Maven Project Step Guide (2020.09.21)

There are four steps in total

Program dependent environment: JDK 1.8, IDEA 200.1, Scala 2.13.3, Maven 3.6.3

The first step is to create a new Scala Maven project

Select Maven on the left side of the New Project interface , the Project SDK on the right will automatically select JDK, check the box on the left side of Create from archetype, mark , and then select org.scala-tools.archetypr:scala-archetype-simple, click Next;

In the pop-up window, enter the project name, the location where the project is saved, and the GroupId, ArtifactId, and Version.

Then select the path of Maven installation, configuration file and respository library path;

The second step is to modify the pom.xml file in the project interface

First, replace the scala.version in the pom.xml file with the locally installed scala version 2.13.3.

Then, delete the part in the pom.xml file, as shown in the following figure:

Finally, change the maven-eclipse-plugin Part is also deleted

The third step is to open Add Framewok Support and add Scala SDK development environment support

In the Add Framewok Support window interface, select Scala, and use library on the right to select the locally installed Scala


The fourth step is to open the project directory, delete the automatically created class of the project, and then create your own class

Find the AppTest and MySpec classes in src->test-> in the directory and delete them,

then create a new package and new class in src->main ( if you still report an error, you need to close the project and reopen it )

// Testmyscala.scala中的代码如下
package org.example

object Testmyscala
{
    
    
  def main(args: Array[String]): Unit =
  {
    
    
    val lines = List("hello java hello python","hello scala","hello scala hello java hello scala")
    // 切分并压平
    val words = lines.flatMap(_.split(" "))
    // 把每个单词生成一个一个的pair
    val tuples = words.map((_,1))
    // 以key(单词)进行分组
    val grouped = tuples.groupBy(_._1)
    // 统计value长度
    val sumed = grouped.mapValues(_.size)
    // 排序
    val sorted = sumed.toList.sortBy(_._2)
    // 降序排列
    val result = sorted.reverse
    println(result)
  }
}

Next, you can find Build->Build Module or Project at the top of the IDEA interface.

Finally, right-click on the Testmyscala.scala interface and select Run to run the Scala program.

The sixth step, type the class into a jar package

First, you need to add the following plugin under build in the pom.xml file:

<build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass></mainClass>
                </transformer>
              </transformers>
              <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <defaultGoal>compile</defaultGoal>
  </build>

Then modify the plugin in the pom.xml file, as shown below:

<mainClass></mainClass>添加为<mainClass>org.example.Testmyscala</mainClass>
There are many ways to form a jar package in IDEA. Here are two of them: Method 1: Build Artifacts in the File->Project Structure interface that comes with IDEA . As shown in the figure below, pay attention to the path of the main manifest file MANIFEST.MF. Under src, **do not select under src\main** Find Build->Build Artifacts at the top of the IDEA interface, select MyScala.jar in the Build Artifact interface, and select Build in the pop-up Action interface; finally the jar package we packed will be Generated in the out directory. Use the cmd window to execute the JAVA jar package program in the out folder of the project, and the test succeeds.

Method 2: Use the Maven plug-in maven-shade-plugin to package, find the hidden Maven on the right side of the IDEA window to open the lifecycle in it, first click Clean, and then click Package. The jar package will be generated in the target directory.

Execute the JAVA jar package program in the target folder of the project, and run successfully after the test.

Guess you like

Origin blog.csdn.net/jing_zhong/article/details/108568660