728x90


빌드를 할 때 리소스를 추가하여 빌드를 해야할 때가 있다.

이번에는 리소스를 추가하여서 빌드하는 법을 보도록하자.


먼저 프로젝트 구조를 보도록하자.

현재 필자가 사용하는 클래스는 3개이며 resources는 하나의 이미지와 하나의 프로퍼티파일을 사용한다.

각각의 내용을 간략하게 보도록하자.


package net.theceres;

public class App {
public static void main(String[] args) {
MyFrame myframe = new MyFrame(500, 300);
myframe.setVisible(true);
}
}

package net.theceres;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

public MyFrame(int width,int height){
setSize(width,height);
setContentPane(new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon ii = new ImageIcon(getClass().getResource("/index.jpeg"));
Image img = ii.getImage();
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
});
setTitle(ResTxt.getS("title"));
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
package net.theceres;

import java.util.ResourceBundle;

public class ResTxt {
private final static String RESOURCE = "main";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(RESOURCE);

public static String getS(String key) {
return RESOURCE_BUNDLE.getString(key);
}
}
#main.properties
title=SwingApp


각각의 파일의 내용은 위와같다.

위 파일이 실행되기 위해서는 총 리소스들이 반드시 필요한 상황이다.

이제 pom.xml을 수정하도록하자.


<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>

리소스를 추가하는 방법은 너무나도 간단하다.

해당 리소스디렉터리를 추가해주면된다.

해당 리로스들의 경로는 이 디렉터리를 루트디렉터리라고 생각하고 작업을 하면된다.

필자가 만든 위의 총 예제 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>net.theceres</groupId>
<artifactId>CommandLineMakeMaven</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>CommandLineMakeMaven</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>net.theceres.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

그럼 테스트 해보도록하자.


제대로 작동하는것을 알수 있는게 그러면 내부에 어떻게 저장되고 있을지 보자.


압축을 해제하면 위와 같이 저장되고 있음을 확인할 수 있따.


+ Recent posts