1. 程式人生 > >JDK9 + Maven + Module 模組化多專案

JDK9 + Maven + Module 模組化多專案

1. 建立POM PARENT, 以及三個MODULE: address, person, dist。dist為最終輸出目錄,用到了maven-assembly-plugin

目錄結構如圖:


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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tony.realsound</groupId>
    <artifactId>JDK9-Demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <modules>
        <module>address</module>
        <module>person</module>
        <module>dist</module>
    </modules>

    <name>JDK9-Demo</name>

    <dependencies>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>1.9</source>
                        <target>1.9</target>
                        <showWarnings>true</showWarnings>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>

address目錄,在java下建立module-info.java:

module jdojo.address {
    exports com.jdojo.address;
}
package com.jdojo.address;

/**
 * Created by Administrator on 2018/5/14.
 */
public class Address {
    private String line1 = "1111 Main Blvd.";
    private String city = "Jacksonville";
    private String state = "FL";
    private String zip = "32256";

    public Address() {
    }

    public Address(String line1, String city, String state, String zip) {
        this.line1 = line1;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }

    public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

}
person目錄,在java下建立module-info.java:
module jdojo.person {
    requires jdojo.address;

    exports com.jdojo.person;
}

修改pom.xml:

    <dependencies>
        <dependency>
            <groupId>com.tony.realsound</groupId>
            <artifactId>address</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

package com.jdojo.person;

import com.jdojo.address.Address;

/**
 * Created by Administrator on 2018/5/14.
 */
public class Person {
    private long personId;
    private String firstName;
    private String lastName;
    private Address address = new Address();

    public Person(long personId, String firstName, String lastName) {
        this.personId = personId;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public long getPersonId() {
        return personId;
    }

    public void setPersonId(long personId) {
        this.personId = personId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getAddress() {
        return address;
    }

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

測試類:

package com.jdojo.person;

import com.jdojo.address.Address;

/**
 * Created by Administrator on 2018/5/14.
 */
public class Main {
    public static void main(String[] args) {
        Person john = new Person(1001, "John", "Jacobs");
        String fName = john.getFirstName();
        String lName = john.getLastName();
        Address addr = john.getAddress();

        System.out.printf("%s %s%n", fName, lName);
        System.out.printf("%s%n", addr.getLine1());
        System.out.printf("%s, %s %s%n", addr.getCity(), addr.getState(), addr.getZip());
    }
}

dist目錄,建立assembly.xml:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>jar</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>

            <!-- Enable access to all projects in the current multimodule build! -->
            <useAllReactorProjects>true</useAllReactorProjects>

            <!-- Now, select which projects to include in this module-set. -->
            <includes>
                <include>com.tony.realsound:address</include>
                <include>com.tony.realsound:person</include>
            </includes>
            <binaries>
                <outputDirectory>/</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

修改pom.xml:

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>distro-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${project.basedir}/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

OK,在parent pom目錄:mvn clean package



maven-assembly-plugin把兩個jar包放在\dist\target\dist-1.0-jar下,這也是module-path。

在parent pom目錄:java --module-path .\dist\target\dist-1.0-jar --module jdojo.person/com.jdojo.person.Main

John Jacobs
1111 Main Blvd.
Jacksonville, FL 32256