Maven learning

1. Installation

1-1. Download

Download "apache-maven-3.6.2-bin.zip" from the following site https://maven.apache.org/download.cgi

1-2. Introduction

Expand and pass the path to bin. This time, I chose "D: \ apache-maven-3.6.2 \ bin".

1-3. Confirmation

mvn -version

OK if you start with. This time, the following was displayed, so I installed the JDK.

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

The downloaded JDK is "Amazon Corretto 8"

Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-28T00:06:16+09:00)
Maven home: D:\apache-maven-3.6.2\bin\..
Java version: 1.8.0_232, vendor: Amazon.com Inc., runtime: C:\Program Files\Amazon Corretto\jdk1.8.0_232\jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

It worked safely.

2. Settings

2-1. Maven settings

Set up a local repository. Open Maven \ conf \ settings.xml and set any location as a local repository as shown below.

settings.xml


<localRepository>D:/apache-maven-3.6.2/repository/</localRepository>

2-2. Eclipse settings

  1. Open the setting screen by selecting [Window]-[Settings].
  2. Select [Maven]-[User Settings].
  3. Specify the settings.xml file in the user settings.

3. Create a project

As an example, create an address book in an SWT application. The application consists of three jars. ・ View ・ Domain ・ Infrastructure image.png

3-1. Creating a parent project

  1. [New]-[Maven Project]
  2. On the "New Maven project" screen, check "Create a simple project" and click "Next".

image.png

  1. Change "Packaging" to [pom], enter appropriate values for group id and artifact id, and click "Done".

image.png

-Group Id: A name that does not overlap with all projects. Package name (domain + project name) -Artifact Id: Jar file name without version

  1. Completed. image.png

3-2. Creating a child project

  1. Select "Parent Project" and right-click.
  2. [New]-[Other]
  3. [Maveb]-[Maven Module]
  4. Enter the "Module Name". image.png
  5. When the creation is completed, the configuration will be as follows. image.png

4. Domain coding

4-1. Java source to store the address

It has a String named Name and Address 1, Address 2, Date named Birthday, and an int for management.

Address.java



package ml.kerotori.app.domain;

import java.util.Date;

public class Address {

	private int no;			//Management number
	private String Name;		//name
	private String Address1;	//Address 1
	private String Address2;	//Address 2
	private Date Birthday;		//birthday
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getAddress1() {
		return Address1;
	}
	public void setAddress1(String address1) {
		Address1 = address1;
	}
	public String getAddress2() {
		return Address2;
	}
	public void setAddress2(String address2) {
		Address2 = address2;
	}
	public Date getBirthday() {
		return Birthday;
	}
	public void setBirthday(Date birthday) {
		Birthday = birthday;
	}

}


4-2. Compile

  1. Right-click on the "Domain" project
  2. [Run]-[Maven install]
  3. I got the following error.
[ERROR]Source option 5 is not currently supported. Please use 6 or later.
[ERROR]Target option 1.5 is not currently supported. 1.Please use 6 or later.
  1. Make Java compilation settings in the pom.xml file of the parent project (parent, not the "Domain" project). Add the following:

pom.xml


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>

The whole picture is as follows.

image.png

  1. This will compile successfully.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.125 s
[INFO] Finished at: 2019-11-03T22:29:41+09:00
[INFO] ------------------------------------------------------------------------

5. Infrastructure coding

Save the address book to an XML file so that it can be read.

5-1. Create Save and Load interfaces

Save.java


package ml.kerotori.app.infrastructure.core;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public interface Save<T> {
	public static <T> boolean saveXml(String fileName, T obj) {
		//Save object data
		try (XMLEncoder encoder = new XMLEncoder(
				new BufferedOutputStream(
						new FileOutputStream(fileName)))) {
			encoder.writeObject(obj);
		} catch (FileNotFoundException e) {
			return false;
		}catch(Exception e) {
			return false;
		}
		return true;
	}
}

Load.java


package ml.kerotori.app.infrastructure.core;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public interface Load<T> {
	@SuppressWarnings("unchecked")
	public static <T>  T loadXml(String fileName) {

		try (XMLDecoder decoder = new XMLDecoder(
				new BufferedInputStream(
						new FileInputStream(fileName)))) {
			return (T) decoder.readObject();
		} catch (FileNotFoundException e) {
			return null;
		}catch(Exception e) {
			return null;
		}
	}
}

5-2. Creating Address Dao

AddressDao.java


package ml.kerotori.app.infrastructure.dao;

import java.io.Serializable;

import ml.kerotori.app.domain.Address;
import ml.kerotori.app.infrastructure.core.Load;
import ml.kerotori.app.infrastructure.core.Save;

public class AddressDao implements Serializable,Save<Address>, Load<Address> {

	private Address address;

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public static Address Load(int no) {
		return Load.loadXml(no + ".xml");
	}

	public boolean Save() {
		return Save.saveXml(this.address.getNo() + ".xml", address);
	}
}

5-2. Domain reference

Add the following to the infrastructure pom.xml.

xml.pom.xml


   <dependencies>
           <dependency>
            <groupId>ml.kerotori.app</groupId>
            <artifactId>Domain</artifactId>
            <version>${project.version}</version>
        </dependency>
   </dependencies>

5-3. Creating test code

AddressTest.java


package ml.kerotori.app.infrastructure.dao;

import static org.junit.jupiter.api.Assertions.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.jupiter.api.Test;

import ml.kerotori.app.domain.Address;

class AddressDaoTest {

	@Test
	void SaveTest1() {
		Address address = new Address();
		address.setNo(1);
		address.setName("Ichiro Sato");
		address.setAddress1("Hokkaido");
		address.setAddress2("Sapporo");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date date = null;
		try {
			date = sdf.parse("1990/10/11");
		} catch (ParseException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}
		address.setBirthday(date);
		AddressDao dao = new AddressDao();
		dao.setAddress(address);
		assertTrue(dao.Save());


	}

	@Test
	void LoadTest1() {
		Address address = AddressDao.Load(1);
		assertEquals("Ichiro Sato", address.getName());

	}
}

5-4. Test pom.xml

pom.xml(Parent project)


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.7.2</version>
				<configuration>
					<workingDirectory>${project.basedir}/src/test/resources</workingDirectory>
				</configuration>
			</plugin>


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.19.1</version>
				<dependencies>
					<dependency>
						<groupId>org.junit.platform</groupId>
						<artifactId>junit-platform-surefire-provider</artifactId>
						<version>1.1.0</version>
					</dependency>
					<dependency>
						<groupId>org.junit.jupiter</groupId>
						<artifactId>junit-jupiter-engine</artifactId>
						<version>5.1.0</version>
					</dependency>
				</dependencies>
			</plugin>

If the test fails, running [Maven]-[Update Project] may help.

6. View coding

6-1. Preparing to create AddressView

  1. Right-click on the "View" project.
  2. [New]-[Other]
  3. [WindowsBuilder]-[SWT Designer]-[SWT]-[Application Window]

6-2. Memo

6-2-1. About SWT

Regarding SWT for Windows, the new version (4.13) is for 64bit and does not work in a 32bit environment. https://download.eclipse.org/eclipse/downloads/drops4/R-4.13-201909161045/ Is the latest 32-bit version 3.8.2 below? https://archive.eclipse.org/eclipse/downloads/drops/R-3.8.2-201301310800/

In the Eclipse Marketplace, enter "WindowBuilder" in the search and install.

6-2-2. Download destination

https://repo1.maven.org/maven2/org/eclipse/platform/org.eclipse.swt.win32.win32.x86/3.108.0/

6-2-3. About clearing Maven's local repository

All I had to do was physically delete the folder.

6-2-4. SWT solution

I gave up and decided to download it myself and manually register it in the repository.

6-3. Reference settings

Set the following in pom.xml of View.

pom.xml


	<profiles>
		<profile>
			<id>windows_x86</id>
			<activation>
				<os>
					<family>Windows</family>
					<arch>x86</arch>
				</os>
			</activation>
			<dependencies>
				<dependency>
					<groupId>swt</groupId>
					<artifactId>swt-win32-x86</artifactId>
					<version>3.8.2</version>
					<type>jar</type>
				</dependency>
			</dependencies>
		</profile>
		<profile>
			<id>windows-x86_64</id>
			<activation>
				<os>
					<family>Windows</family>
					<arch>amd64</arch>
				</os>
			</activation>
			<dependencies>
				<dependency>
					<groupId>swt</groupId>
					<artifactId>swt-win32-x86_64</artifactId>
					<version>3.8.2</version>
					<type>jar</type>
				</dependency>
			</dependencies>
		</profile>
	</profiles>

Download "swt-3.8.2-win32-win32-x86.zip" and "swt-3.8.2-win32-win32-x86_64.zip", and unzip them to find "swt.jar". Change the name as follows and save it in the "D: \ swt" folder. swt-3.8.2-win32-x86.jar swt-3.8.2-win32-x86_64.jar

Manually register to the repository with the following command.

SET REPO_URL=file:D:/apache-maven-3.6.2/repository/
mvn deploy:deploy-file -DrepositoryId=ml.kerotori.app -Durl=%REPO_URL% -Dfile=d:\swt\swt-3.8.2-win32-x86.jar -DgroupId=swt -DartifactId=swt-win32-x86 -Dversion=3.8.2 -Dpackaging=jar
mvn deploy:deploy-file -DrepositoryId=ml.kerotori.app -Durl=%REPO_URL% -Dfile=d:\swt\swt-3.8.2-win32-x86_64.jar -DgroupId=swt -DartifactId=swt-win32-x86_64 -Dversion=3.8.2 -Dpackaging=jar

The error did not disappear for some reason, but when I cleared the error and executed it, it worked normally. image.png

6-4. Creating AddressView

For the time being, only normal operation. Input value check is omitted.

AddressView.java


package ml.kerotori.app.view;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ml.kerotori.app.domain.Address;
import ml.kerotori.app.infrastructure.dao.AddressDao;

public class AddressView {

	protected Shell shell;
	private Text txtNo;
	private Text txtName;
	private Text txtAddress1;
	private Text txtAddress2;
	private Text txtBirtyday;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AddressView window = new AddressView();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new GridLayout(2, false));

		Label lblNo = new Label(shell, SWT.NONE);
		lblNo.setText("No");
		txtNo = new Text(shell, SWT.BORDER);
		txtNo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label label = new Label(shell, SWT.NONE);
		label.setText("name");
		txtName = new Text(shell, SWT.BORDER);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("Address 1");
		txtAddress1 = new Text(shell, SWT.BORDER);
		txtAddress1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setText("Address 2");
		txtAddress2 = new Text(shell, SWT.BORDER);
		txtAddress2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setText("birthday");
		txtBirtyday = new Text(shell, SWT.BORDER);
		txtBirtyday.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnSave = new Button(shell, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				AddressDao dao = new AddressDao();
				Address address = new Address();
				address.setNo(Integer.parseInt(txtNo.getText()));
				address.setName(txtName.getText());
				address.setAddress1(txtAddress1.getText());
				address.setAddress2(txtAddress2.getText());
				address.setBirthday(new Date(txtBirtyday.getText()));
				dao.setAddress(address);
				if(dao.Save()) {
					MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
					msgBox.setText("MessageBox");
					msgBox.setMessage("Successful registration");
					int reply = msgBox.open();
				}else {
					MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
					msgBox.setText("MessageBox");
					msgBox.setMessage("Registration failure");
					int reply = msgBox.open();
				}

			}
		});
		btnSave.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1));
		btnSave.setText("Registration");

	}

}

6-5. Reference to Domain and Infrastructure

pom.xml


<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>
	<parent>
		<groupId>ml.kerotori.app</groupId>
		<artifactId>SWTsample</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>View</artifactId>

	<profiles>
		<profile>
			<id>windows_x86</id>
			<activation>
				<os>
					<family>Windows</family>
					<arch>x86</arch>
				</os>
			</activation>
			<dependencies>
				<dependency>
					<groupId>swt</groupId>
					<artifactId>swt-win32-x86</artifactId>
					<version>3.8.2</version>
					<type>jar</type>
				</dependency>
			</dependencies>
		</profile>
		<profile>
			<id>windows-x86_64</id>
			<activation>
				<os>
					<family>Windows</family>
					<arch>amd64</arch>
				</os>
			</activation>
			<dependencies>
				<dependency>
					<groupId>swt</groupId>
					<artifactId>swt-win32-x86_64</artifactId>
					<version>3.8.2</version>
					<type>jar</type>
				</dependency>
			</dependencies>
		</profile>
	</profiles>

	<dependencies>
		<dependency>
			<groupId>ml.kerotori.app</groupId>
			<artifactId>Domain</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>ml.kerotori.app</groupId>
			<artifactId>Infrastructure</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>
</project>

7. Execution module

If you make pom.xml of the parent project as follows, a module is created in View.

pom.xml


<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>ml.kerotori.app</groupId>
	<artifactId>SWTsample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>View</module>
		<module>Infrastructure</module>
		<module>Domain</module>
	</modules>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.1.1</version>
				<configuration>
					<finalName>SWTsample</finalName>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>ml.kerotori.app.view.AddressView</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.1</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<!-- configure the plugin here -->
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


</project>

8. Commit to GitHub

Set with reference to the following. https://qiita.com/cotrpepe/items/7cafaacb538425a78f1f

For your reference. https://github.com/kero1976/SWTsample

Recommended Posts

Maven learning
Java learning (0)
Ruby learning 4
Ruby learning 5
Servlet learning
Ruby learning 3
Learning output ~ 11/3 ~
Ruby learning 2
Ruby learning 6
Learning output
[Maven] About Maven
Ruby learning 1
Create Maven Project
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
For JAVA learning (2018-03-16-01)
Try using Maven
Ruby Learning # 1 Introduction
Java learning day 5
java, maven memo
Ruby Learning # 14 Hashes
Rails learning day 3
Rails learning day 4
Ruby Learning # 33 Inheritance
Rails learning day 2
Ruby Learning # 15 Methods
Maven goal types
Maven3 error memo
rails learning day 1
java learning day 2
java learning day 1