이번엔 메이븐 멀티 모듈을 알아보겠다.
아까 친구(?) 나는 친군데 자꾸 형이라 부른다. 멀티 모듈을 잘모른다 하길래.. 나도 한번 만들기만 해봐서 다시 한번 정리 해보겠다.
요즘 많이 쓰긴 하는데 큰 프로젝트가 아니라면 오히려 부담만 주는거 같다. 필자 생각
단일 프로젝트로 만들다 보면 중복 코드가 발생할 수 있는데 멀티 모듈로 이를 해결할 수 있다. 이거 너무 좋은 장점이다.
물론 잘 사용해야지 잘 못사용하면 오히려 독이 될 수도 있을 거 같다.
각성하고 필자는 인텔리j 기준으로 작성하겠다.

groupid와 artifactid를 입력하고 next를 누르자

프로젝트명을 입력한후에 완료를 누르자
그런후에 프로젝트 오른쪽 클릭후에 모듈을 선택하자

그럼 다음과 같이 창이 뜨는데 첨에 했던 방식과 동일하다.

동일한 형식으로 각자의 맞게 입력하자

그럼 다음과 같이 나오는데 부모 pom.xml 부터 보자.
<modules>
<module>core</module>
</modules>
부모 모듈에는 자식들의 모듈을 포함하고 있다.
<packaging>pom</packaging>
그리고 packaging이 jar나 war가 아니고 pom으로 되어있다. 실질적으로 부모의 역할은 이게 끝이다. 물론 부모의 디펜더시를 하면 자식들까지 사용가능하다.

다음은 자식 모듈이다. 눈에 띄는건 parent라는 태그다. 메이븐에는 부모를 설정할 수 있다. Spring boot를 해봤으면 다 아는 내용이다.
<parent>
<artifactId>maven-multi-module</artifactId>
<groupId>me.wonwoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
그럼 한번 잘 되나 확인하고 위해 spring boot를 설정했다. 부트는 사랑이다.
일단 web이라는 모듈또한 만들었다. 아까와 동일한 방식으로..
부모 pom에는 테스트등 공통적으로 사용되는 디펜더시를 추가 하면 된다. 간단하게 하기 위해 필자는 테스트만 추가했다.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
코어에는 repository가 존재 하므로 데이터 베이스와 연관된 디펜더시를 추가하자
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
web에는 실제 어플리케이션이 작동해야되므로 web을 추가 하였다
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
그런데 코어에는 service와 repository를 web에는 controller를 작성할 것이다.
그런데 controller에서 service 혹은 repository 사용해야 되므로 web에 있는 pom.xml에 다음과 같이 추가 해야된다. 코어를 디펜더시 하는거다. 그러면 service와 repository 등 core에 있는 코드를 재사용할 수 있다.
<dependency>
<groupId>me.wonwoo</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
일단 셋팅은 끝났다. 구현해보자. 딱히 설명은 쓰지 않겠다. 잘 돌아가나 확인차 하는 것이니.
web
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private AccountRepository accountRepository;
@Bean
public CommandLineRunner commandLineRunner() {
return args ->
Arrays.asList(
new Account("wonwoo", "123"),
new Account("youngjin", "0000")
).forEach(accountRepository::save);
}
}
...
@RestController
class AccountController {
@Autowired
private AccountRepository accountRepository;
@RequestMapping("/account")
public List<Account> account(){
return accountRepository.findAll();
}
}
core
@Entity
public class Account {
@Id
@GeneratedValue
private Long id;
private String name;
private String password;
public Account(){ //jpa
}
public Account(String name, String password) {
this.name = name;
this.password = password;
}
...
//getter setter
}
@Service
@Transactional
public class AccountService {
}
다이렉트로 레포지토리를 사용해서 서비스 코드가 없다.ㅠㅠ
public interface AccountRepository extends JpaRepository<Account, Long>{
}
한번 실행 해 보자
http://localhost:8080/account
웹브라우저에 json이 뜬다면 성공한 거다.
해당 소스는
깃헙에 올라가 있다.