This is the procedure for Java beginners to create a program in Intellij, package it, and make something that works for the time being.
This time, we will create a program that only acquires the content of the title on the top page of Keta. Rather than studying Java, it is more like a tutorial for understanding the flow of Java development.
Please download and install jdk and Intellij from the above link.
After launching Intellij, click Create New Project
Select Maven
and click Next
Enter the following values and click Next
item | value |
---|---|
GroupId | jp.sample |
ArtifactId | sample-artifact |
Version | Default |
Enter the Project name and the location where you want to place the Project files and click Finish
If the following screen is displayed, it is successful.
File for project management. Is it something like Gemfile in Rails?
Create with the following contents.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jp.sample</groupId>
<artifactId>sample-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>sample-${project.version}</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>jp.sample.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After adding to pom.xml,
If you see Maven projects need to be imported
in the bottom right, click ʻEnable Auto-Import`
dependencies Dependent library settings. This time, we will use the following libraries.
Library name | Use |
---|---|
Jsoup | For scraping |
junit | for test |
properties Described to avoid the following errors during build.
[ERROR] Source option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
maven-assembly-plugin A plugin for creating packages that include all dependencies. Specify Main-Class in archive-> manifest. This eliminates the need for Manifest.MF.
<archive>
<manifest>
<mainClass>jp.sample.Main</mainClass>
</manifest>
</archive>
Create a scraping program.
First, create a Java Class.
Create a class named Main
.
Once created, write the following in Main.java.
Main.java
package jp.sample;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
final ScrapeService scrapeService = new ScrapeService();
System.out.println(scrapeService.getTitle());
}
}
Create ScrapeService.java in the same way. It is a program to get the title of Keta.
ScrapeService.java
package jp.sample;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
public class ScrapeService {
private String url = "https://qiita.com/";
public String getTitle() throws IOException {
Document document = Jsoup.connect(url).get();
Elements title = document.select("title");
return title.text();
}
}
Open Main.java, click the play button to the left of public class Main {
, and click Run'Main.main ()'
to run the program.
Then, the following result will be displayed in the debugger below. If the title of Keta is displayed, it is successful.
Next, write the test code.
Open ScrapeService.java, with the cursor on the class name ScrapeService
, and press ʻAlt + ʻEnter
.
Then, as shown in the image below, the option Create Test
will be displayed. Click it.
This time, I will use JUnit4. If you see a button called FIX
, click it.
There is a list of testable methods under Generate test methods for:
, check them and click OK.
Then, the test code file is created as shown below.
Then edit it as follows.
LoginServiceTest.java
package jp.sample;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class ScrapeServiceTest {
@Test
public void getTitle() throws IOException {
final ScrapeService scrapeService = new ScrapeService();
assertEquals("Qiita - A technical knowledge sharing platform for programmers.", scrapeService.getTitle());
}
}
Click the play button to the left of public class LoginServiceTest {
and click Run'Main.main ()'
to run the test.
If it is displayed in the debugger as shown below, the test is successful.
build Build and package. From here on, you'll be working from the command line, not Intellij.
Go to the sampleartifact / directory where pom.xml is located and run the following command.
$ mvn package
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running jp.sample.ScrapeServiceTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.786 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sample-artifact ---
[INFO] Building jar: /Users/username/IdeaProjects/sampleartifact/target/sample-artifact-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-assembly-plugin:3.1.0:single (make-assembly) @ sample-artifact ---
[INFO] Building jar: /Users/username/IdeaProjects/sampleartifact/target/sample-1.0-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.910 s
[INFO] Finished at: 2017-12-28T18:10:59+09:00
[INFO] Final Memory: 20M/67M
[INFO] ------------------------------------------------------------------------
A jar file called sample-1.0-SNAPSHOT-jar-with-dependencies.jar
is generated in sampleartifact / target, so try running it.
$ java -jar sample-1.0-SNAPSHOT-jar-with-dependencies.jar
Qiita - A technical knowledge sharing platform for programmers.
If the title of Keta is displayed like this, it is successful!
mvn install
mvn install
will generate files not only under target
, but also in the Local repository under ~ / .m2 /
.
$ mvn install
$ ls -1 -a ~/.m2/repository/jp/sample/sample-artifact/1.0-SNAPSHOT/
.
..
_remote.repositories
maven-metadata-local.xml
sample-artifact-1.0-SNAPSHOT-jar-with-dependencies.jar
sample-artifact-1.0-SNAPSHOT.jar
sample-artifact-1.0-SNAPSHOT.pom
You can delete it with the following command. It is used to empty the target and then rebuild it.
$ mvn clean
Recommended Posts