Tìm các thư viện (dependencies)

Nội dung

Hướng dẫn tìm kiếm Maven dependencies với Maven Repository.

Các bước thực hiện

Bước 1: Truy cập vào trang http://mvnrepository.com/

Bước 2: Điền thư viện cần tìm kiếm vào thanh search

search_dependencies_maven_step_2a search_dependencies_maven_step_2b search_dependencies_maven_step_2c search_dependencies_maven_step_2d

Bước 3: Copy đoạn XML vào file Pom.xml

Thêm các thư viện (dependencies) vào Maven Project

Nội dung

Tạo một project sử dụng Gson để chuyển đổi Object ra Json

Các phần mềm cần thiết

  • JDK 1.6 trở lên
  • Maven 3.0 trở lên
  • NetBeans 7.1 trở lên

Các bước tạo project

Bước 1: Tạo một Maven Java Application Project.

Bước 2: Mở file Pom.xml

java_application_maven_with_dependencies_step_2a

Thêm đoạn mã sau vào file Pom.xml

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.4</version>
</dependency>

java_application_maven_with_dependencies_step_2b

Bước 3: Build project để download và nạp thư viện

Bước 4: Thay đổi nội dung file App.java thành

import com.google.gson.Gson;
public class App {
    public static class NhanVien {
        public String HoTen;
        public int Tuoi;
    }
    public static void main(String[] args) {
        NhanVien nhanVien = new NhanVien();
        nhanVien.HoTen = “Jeremy”;
        nhanVien.Tuoi = 22;
        Gson gson = new Gson();
        System.out.println(gson.toJson(nhanVien));
    }
}

Bước 5: Build và chạy thử project

java_application_maven_with_dependencies_step_5a

Tải MavenWithDependenciesProject tại đây

Build Java Application có sử dụng thư viện (dependencies)

Nội dung

Build Java Application có sử dụng thư viện trong Maven

Các bước thực hiện

Bạn có thể làm theo một trong hai cách dưới.

Copy tất cả dependencies thành 1 file jar

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>{Main Class}</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Copy tất cả dependencies vào một thư mục lib

<build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>{Main Class}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>