Javaéçºã®äžçãžããããïŒå€ãã®Javaãããžã§ã¯ãã§ã¯ãã©ã€ãã©ãªã®ç®¡çããã«ãããã»ã¹ãè€éã«ãªããã¡ã§ããããã§ç»å Žããã®ã Apache Maven ã§ããMavenã¯ãJavaãããžã§ã¯ãã®ãã«ãèªååãšäŸåé¢ä¿ç®¡çãåçã«ç°¡ç¥åããŠããã匷åãªããŒã«ã§ãããã®èšäºã§ã¯ãMavenã®åºæ¬çãªæŠå¿µãã䜿ãæ¹ãŸã§ãåå¿è ã«ãåããããã解説ããŠãããŸãããããMavenã®äžçãæ¢æ€ããŸãããïŒâš
1. Mavenãšã¯äœãïŒ ð€
Mavenã¯ãApache Software Foundationã«ãã£ãŠéçºããããäž»ã«Javaãããžã§ã¯ãåãã®ãããžã§ã¯ã管çããã³ãã«ãèªååããŒã«ã§ãã2004幎ã«ããŒãžã§ã³1ããªãªãŒã¹ããããã®åŸæ¹è¯ãéããããçŸåšã¯ããŒãžã§ã³3ç³»ãå®å®çãšããŠåºãå©çšãããŠãããããŒãžã§ã³4ã®éçºãé²ãã§ããŸããMavenç»å Žä»¥åã¯ãApache Antãªã©ã®ããŒã«ã䜿ãããŠããŸããããèšå®ãç ©éã«ãªããã¡ã§ãããMavenã¯ãèšå®ããèŠçŽ (Convention over Configuration)ããšããææ³ã«åºã¥ããæšæºçãªãããžã§ã¯ãæ§é ãã©ã€ããµã€ã¯ã«ãå°å ¥ããããšã§ãèšå®ãç°¡ç¥åããéçºå¹çãåäžãããŸãã
Mavenã解決ããäž»ãªèª²é¡ã¯ä»¥äžã®2ã€ã§ãã
- ãã«ãããã»ã¹ã®æšæºåãšèªåå: ã³ã³ãã€ã«ããã¹ããããã±ãŒãžã³ã°ïŒJAR/WARãã¡ã€ã«ã®äœæïŒããããã€ãšãã£ãäžé£ã®ãã«ãäœæ¥ããç°¡åãªã³ãã³ãã§å®è¡ã§ããããã«ããŸããããã«ãããæäœæ¥ã«ãããã¹ãé²ãã誰ãå®è¡ããŠãåãçµæãåŸãããåçŸæ§ã®é«ããã«ããå®çŸããŸãã
- äŸåé¢ä¿ç®¡çã®ç°¡ç¥å: ãããžã§ã¯ããå¿
èŠãšããå€éšã©ã€ãã©ãªïŒäŸåé¢ä¿ïŒããèšå®ãã¡ã€ã«ïŒ
pom.xml
ïŒã«èšè¿°ããã ãã§ãMavenãèªåçã«ããŠã³ããŒããããããžã§ã¯ãã«è¿œå ããŠãããŸããã©ã€ãã©ãªã®ããŒãžã§ã³ç®¡çããã©ã€ãã©ãªãããã«äŸåããŠããå¥ã®ã©ã€ãã©ãªïŒæšç§»çäŸåé¢ä¿ïŒã®è§£æ±ºãèªåã§è¡ããããããéçºè ã¯ã©ã€ãã©ãªç®¡çã®æéãã解æŸãããŸãã
ãããã®æ©èœã«ãããç¹ã«å€§èŠæš¡ãªãããžã§ã¯ããããŒã éçºã«ãããŠãMavenã¯éåžžã«åŒ·åãªå©ããšãªããŸãããããžã§ã¯ãã®æ§é ãæšæºåããããããæ°ããã¡ã³ããŒããããžã§ã¯ããç解ãããããªããŸãã
2. Mavenã®ã»ããã¢ãã ð»
Mavenãå©çšããã«ã¯ããŸãã䜿ãã®ç°å¢ã«Mavenãã€ã³ã¹ããŒã«ããå¿ èŠããããŸããææ°ã®å®å®ç㯠Maven 3.9.9 (2024幎8æ17æ¥ãªãªãŒã¹) ã§ããMaven 3.9以éãå®è¡ããã«ã¯ãJDK 8ä»¥äž ãå¿ èŠã§ãã
2.1. ããŠã³ããŒã
Apache Mavenã®å ¬åŒãµã€ã ãããææ°çã®ãã€ããªïŒBinary zip archive ãŸã㯠Binary tar.gz archiveïŒãããŠã³ããŒãããŸãã
2.2. ã€ã³ã¹ããŒã«
- ããŠã³ããŒãããã¢ãŒã«ã€ããã¡ã€ã«ããä»»æã®ãã£ã¬ã¯ããªïŒäŸ: Windowsãªã
C:\tools
ãmacOS/Linuxãªã/usr/local
ã~/tools
ãªã©ïŒã«å±éããŸããå±éåŸã®ãã£ã¬ã¯ããªïŒäŸ:apache-maven-3.9.9
ïŒãMavenã®ã€ã³ã¹ããŒã«ãã£ã¬ã¯ããªïŒMAVEN_HOME
ïŒã«ãªããŸãã -
ç°å¢å€æ°ã®èšå®:
MAVEN_HOME
ïŒãŸãã¯M2_HOME
ïŒ: Mavenã®ã€ã³ã¹ããŒã«ãã£ã¬ã¯ããªãæå®ããŸããPATH
:%MAVEN_HOME%\bin
(Windows) ãŸãã¯$MAVEN_HOME/bin
(macOS/Linux) ãæ¢åã®PATHã«è¿œå ããŸãã
Windowsã§ã®èšå®äŸ (ã³ãã³ãããã³ãã):
setx MAVEN_HOME "C:\tools\apache-maven-3.9.9" /M setx PATH "%MAVEN_HOME%\bin;%PATH%" /M
管çè æš©éã§å®è¡ããå¿ èŠããããŸããèšå®åŸã¯ã³ãã³ãããã³ãããåèµ·åããŠãã ããã
macOS/Linuxã§ã®èšå®äŸ (
.bash_profile
ã.zshrc
ãªã©):export MAVEN_HOME=~/tools/apache-maven-3.9.9 export PATH=$MAVEN_HOME/bin:$PATH
èšå®ãã¡ã€ã«ãä¿ååŸã
source ~/.bash_profile
ãsource ~/.zshrc
ãå®è¡ããããã¿ãŒããã«ãåèµ·åããŠãã ããã
2.3. åäœç¢ºèª
ã³ãã³ãããã³ãããŸãã¯ã¿ãŒããã«ãéãã以äžã®ã³ãã³ããå®è¡ããŸããMavenã®ããŒãžã§ã³æ å ±ã衚瀺ãããã°ãã€ã³ã¹ããŒã«ã¯æåã§ããð
mvn -v
以äžã®ãããªåºåã衚瀺ãããŸãïŒããŒãžã§ã³çªå·ãJavaã®ããŒãžã§ã³ã¯ç°å¢ã«ãã£ãŠç°ãªããŸãïŒã
Apache Maven 3.9.9 (...)
Maven home: /path/to/apache-maven-3.9.9
Java version: 11.0.x, vendor: ..., runtime: ...
Default locale: ..., platform encoding: ...
OS name: "...", version: "...", arch: "...", family: "..."
2.4. (ãªãã·ã§ã³) èšå®ãã¡ã€ã«ã®ã«ã¹ã¿ãã€ãº
Mavenã®å
šäœçãªèšå®ã¯ settings.xml
ãã¡ã€ã«ã§è¡ããŸãããã®ãã¡ã€ã«ã¯éåžžã以äžã®å Žæã«ãããŸãã
- ã°ããŒãã«èšå®:
${MAVEN_HOME}/conf/settings.xml
(Mavenã€ã³ã¹ããŒã«ãã£ã¬ã¯ããªå ) - ãŠãŒã¶ãŒèšå®:
${user.home}/.m2/settings.xml
(ãŠãŒã¶ãŒã®ããŒã ãã£ã¬ã¯ããªé äžã®.m2
ãã©ã«ãå )
ãŠãŒã¶ãŒèšå®ãã¡ã€ã«ã¯ããã©ã«ãã§ã¯ååšããªããããå¿ èŠã«å¿ããŠã°ããŒãã«èšå®ãã¡ã€ã«ãã³ããŒããŠäœæããŸãããŠãŒã¶ãŒèšå®ã¯ã°ããŒãã«èšå®ãäžæžãããŸãã
settings.xml
ã§ã¯ãäž»ã«ä»¥äžã®èšå®ãè¡ããŸãã
- ããŒã«ã«ãªããžããªã®å Žæ: MavenãããŠã³ããŒãããã©ã€ãã©ãªãªã©ãä¿åããå Žæãããã©ã«ãã¯
${user.home}/.m2/repository
ã§ãããå€æŽãããå Žåã¯<localRepository>
ã¿ã°ã§æå®ããŸãã - ãããã·èšå®: ãããã·ç°å¢äžã§Mavenã䜿çšããå Žåã
<proxies>
ã¿ã°ã§ãããã·ãµãŒããŒã®æ å ±ãèšå®ããŸãã - ãã©ãŒãªããžããª: Maven Central Repositoryãžã®ã¢ã¯ã»ã¹ãé
ãå Žåããçµç¹å
ã®ãªããžããªã䜿çšããå Žåã«ã
<mirrors>
ã¿ã°ã§ä»£æ¿ãªããžããªãæå®ããŸãã
ããŒã«ã«ãªããžããªå€æŽäŸ (settings.xml):
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>D:/path/to/your/local/repo</localRepository>
...
</settings>
3. Mavenã®åºæ¬æŠå¿µ ð¡
Mavenãå¹æçã«äœ¿ãããã«ã¯ãããã€ãã®éèŠãªæŠå¿µãç解ããå¿ èŠããããŸãã
3.1. POM (Project Object Model)
Mavenãããžã§ã¯ãã®äžå¿ãšãªãã®ã pom.xml
ãã¡ã€ã«ã§ããããã¯ãããžã§ã¯ãã«é¢ããããããæ
å ±ïŒãããžã§ã¯ãã®åºæ¬æ
å ±ãäŸåé¢ä¿ããã«ãèšå®ããã©ã°ã€ã³ãªã©ïŒãXML圢åŒã§èšè¿°ãããã®ã§ããããããžã§ã¯ããªããžã§ã¯ãã¢ãã«ããšããååã®éãããããžã§ã¯ãããªããžã§ã¯ããšããŠæãããã®å±æ§ãæ§æãå®çŸ©ããŸãã
pom.xml
ã®äž»ãªèŠçŽ ãèŠãŠã¿ãŸãããã
èŠçŽ å | 説æ | äŸ |
---|---|---|
<modelVersion> |
POMã®ããŒãžã§ã³ãçŸåšã¯éåžž 4.0.0 ãæå®ããŸãã |
<modelVersion>4.0.0</modelVersion> |
<groupId> |
ãããžã§ã¯ããäœæããçµç¹ãã°ã«ãŒããèå¥ããIDãéåžžãéãã¡ã€ã³å圢åŒïŒäŸ: com.example ïŒã§æå®ããŸãã |
<groupId>com.example.myapp</groupId> |
<artifactId> |
ãããžã§ã¯ãïŒææç©ïŒåºæã®IDãéåžžããããžã§ã¯ãåãã¢ãžã¥ãŒã«åãæå®ããŸããgroupId ãš artifactId ã®çµã¿åããã§ãããžã§ã¯ããäžæã«èå¥ããŸãã |
<artifactId>my-app-core</artifactId> |
<version> |
ãããžã§ã¯ãã®ããŒãžã§ã³ã1.0.0 , 1.0-SNAPSHOT ã®ãããªåœ¢åŒã§æå®ããŸãã-SNAPSHOT ã¯éçºäžã®ããŒãžã§ã³ã瀺ããŸãã |
<version>1.0.0</version> |
<packaging> |
ãããžã§ã¯ãã®ææç©ã®åœ¢åŒãããã©ã«ã㯠jar ã§ãããWebã¢ããªã±ãŒã·ã§ã³ãªã war ãPOMèªäœãé
åžããå Žå㯠pom ãªã©ãæå®ããŸãã |
<packaging>jar</packaging> |
<name> |
ãããžã§ã¯ãã®äººéå¯èªãªååã | <name>My Application Core</name> |
<description> |
ãããžã§ã¯ãã®èª¬æã | <description>Core module for my application.</description> |
<properties> |
POMå
ã§å
±éããŠå©çšããå€ãå®çŸ©ããŸããJavaã®ããŒãžã§ã³ãã©ã€ãã©ãªã®ããŒãžã§ã³ãªã©ãå®çŸ©ããããšãå€ãã§ãã${propertyName} 圢åŒã§åç
§ã§ããŸãã |
<properties><java.version>11</java.version></properties> |
<dependencies> |
ãããžã§ã¯ããäŸåããã©ã€ãã©ãªïŒäŸåé¢ä¿ïŒããªã¹ãããŸãã詳现ã¯åŸè¿°ããŸãã | <dependencies>...</dependencies> |
<dependencyManagement> |
äŸåé¢ä¿ã®ããŒãžã§ã³ãäžå
管çããããã®ã»ã¯ã·ã§ã³ããã«ãã¢ãžã¥ãŒã«ãããžã§ã¯ããªã©ã§åœ¹ç«ã¡ãŸããããã§ããŒãžã§ã³ãæå®ããŠãããšã<dependencies> ã§ããŒãžã§ã³ãçç¥ã§ããŸãã |
<dependencyManagement>...</dependencyManagement> |
<build> |
ãã«ãã«é¢ããèšå®ïŒãœãŒã¹ãã£ã¬ã¯ããªãåºåãã£ã¬ã¯ããªã䜿çšãããã©ã°ã€ã³ãªã©ïŒãèšè¿°ããŸãã | <build>...</build> |
<plugins> |
ãã«ãæã«äœ¿çšããMavenãã©ã°ã€ã³ãšãã®èšå®ãèšè¿°ããŸãã<build> ã¿ã°å
ã«èšè¿°ããŸãã |
<plugins>...</plugins> |
<reporting> |
ãããžã§ã¯ãã¬ããŒãçæã«é¢ããèšå®ïŒäœ¿çšããã¬ããŒããã©ã°ã€ã³ãªã©ïŒãèšè¿°ããŸãã | <reporting>...</reporting> |
åºæ¬ç㪠pom.xml
ã®äŸ:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>my-cool-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Cool Application</name>
<description>A sample application to demonstrate Maven basics.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<junit.version>5.9.1</junit.version> <!-- ããããã£ã§ããŒãžã§ã³ã管ç -->
</properties>
<dependencies>
<!-- JUnit 5 dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version> <!-- ããããã£åç
§ -->
<scope>test</scope> <!-- ãã®äŸåé¢ä¿ã¯ãã¹ãæã«ã®ã¿å¿
èŠ -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin: JavaããŒãžã§ã³ãæå® -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <!-- ãã©ã°ã€ã³ã®ããŒãžã§ã³ãæå®æšå¥š -->
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<!-- Maven Surefire Plugin: ãã¹ãå®è¡çš -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
</project>
3.2. ãªããžããª
Mavenã¯ããããžã§ã¯ãã®ææç©ïŒJARãã¡ã€ã«ãªã©ïŒãäŸåé¢ä¿ã©ã€ãã©ãªããã©ã°ã€ã³ãªã©ããªããžããªãšåŒã°ããå Žæã«æ ŒçŽãã管çããŸãããªããžããªã«ã¯äž»ã«3ã€ã®çš®é¡ããããŸãã
-
ããŒã«ã«ãªããžããª:
- éçºè ã®ãã·ã³äžã«ååšãããªããžããªã
- Mavenããªã¢ãŒããªããžããªããããŠã³ããŒãããäŸåé¢ä¿ããã©ã°ã€ã³ãããŒã«ã«ã§ãã«ããããããžã§ã¯ãã®ææç©ïŒ
mvn install
å®è¡æïŒãªã©ããã£ãã·ã¥ãšããŠä¿åãããŸãã - ããã©ã«ãã®å Žæã¯ãŠãŒã¶ãŒããŒã ãã£ã¬ã¯ããªäžã®
.m2/repository
ã§ãããsettings.xml
ã§å€æŽå¯èœã§ãã - äžåºŠããŠã³ããŒããããã¢ãŒãã£ãã¡ã¯ãã¯ããŒã«ã«ãªããžããªã«ä¿åãããããã次å以éã¯é«éã«ã¢ã¯ã»ã¹ã§ããŸãã
-
ã»ã³ãã©ã«ãªããžããª:
- Mavenã³ãã¥ããã£ã«ãã£ãŠæäŸã»ç®¡çãããŠãã巚倧ãªå ¬éãªã¢ãŒããªããžã㪠(https://repo.maven.apache.org/maven2/)ã
- äžçäžã®å€ãã®ãªãŒãã³ãœãŒã¹ã©ã€ãã©ãªãããã«éããããŠããŸãã
- Mavenã¯ããã©ã«ãã§ãã®ãªããžããªãåç §ããå¿ èŠãªäŸåé¢ä¿ãããŠã³ããŒãããŸãã
- éåžžãMaven Repository ãªã©ã®æ€çŽ¢ãµã€ããå©çšããŠã©ã€ãã©ãªãæ¢ããŸãã
-
ãªã¢ãŒããªããžããª:
- ã»ã³ãã©ã«ãªããžããªä»¥å€ã®ããããã¯ãŒã¯äžã«ãããªããžããªã®ç·ç§°ã
- çµç¹å ã§ç¬èªã«æ§ç¯ãããã©ã€ããŒããªããžããªïŒäŸ: Nexus Repository Manager, JFrog ArtifactoryïŒããç¹å®ã®ãã³ããŒãæäŸãããªããžããªãªã©ãå«ãŸããŸãã
pom.xml
ã®<repositories>
ã¿ã°ãsettings.xml
ã®<mirrors>
ã¿ã°ã§èšå®ããããšã§ãMavenããããã®ãªããžããªãåç §ããããã«ãªããŸãã
Mavenã¯ãäŸåé¢ä¿ãå¿ èŠã«ãªããšããŸãããŒã«ã«ãªããžããªãæ¢ããŸããèŠã€ãããªãå Žåã¯ãèšå®ãããŠãããªã¢ãŒããªããžããªïŒããã©ã«ãã§ã¯ã»ã³ãã©ã«ãªããžããªïŒã«ã¢ã¯ã»ã¹ããŠããŠã³ããŒãããããŒã«ã«ãªããžããªã«ä¿åããŸãã
3.3. äŸåé¢ä¿ç®¡ç (Dependency Management)
Mavenã®æã匷åãªæ©èœã®äžã€ãäŸåé¢ä¿ç®¡çã§ããpom.xml
ã® <dependencies>
ã»ã¯ã·ã§ã³ã«ããããžã§ã¯ããå¿
èŠãšããã©ã€ãã©ãªãèšè¿°ããã ãã§ãMavenãèªåçã«è§£æ±ºããŠãããŸãã
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId> <!-- ã©ã€ãã©ãªã®ã°ã«ãŒãID -->
<artifactId>commons-lang3</artifactId> <!-- ã©ã€ãã©ãªã®ã¢ãŒãã£ãã¡ã¯ãID -->
<version>3.12.0</version> <!-- ã©ã€ãã©ãªã®ããŒãžã§ã³ -->
<scope>compile</scope> <!-- äŸåé¢ä¿ã®ã¹ã³ãŒã (çç¥å¯èœãããã©ã«ãã¯compile) -->
</dependency>
<!-- ä»ã®äŸåé¢ä¿ãããã«è¿œå -->
</dependencies>
æšç§»çäŸåé¢ä¿ (Transitive Dependencies)
è¿œå ããã©ã€ãã©ãªãããã«å¥ã®ã©ã€ãã©ãªã«äŸåããŠããå ŽåãMavenã¯ãã®äŸåé¢ä¿ãèªåçã«è§£æ±ºããããŠã³ããŒãããŠãããŸãããããæšç§»çäŸåé¢ä¿ãšåŒã³ãŸããäŸãã°ãã©ã€ãã©ãªAãã©ã€ãã©ãªBã«äŸåããã©ã€ãã©ãªBãã©ã€ãã©ãªCã«äŸåããŠããå Žåãpom.xml
ã«ã©ã€ãã©ãªAãè¿œå ããã ãã§ãBãšCãèªåçã«å©çšå¯èœã«ãªããŸããããã«ãããäŸåé¢ä¿ã®èšè¿°ãæå°éã«æããããšãã§ããŸãã
ãã ããæšç§»çäŸåé¢ä¿ã«ãã£ãŠæå³ããªãããŒãžã§ã³ã®ã©ã€ãã©ãªãå°å
¥ãããããããŒãžã§ã³ç«¶åãçºçãããããããšããããŸããmvn dependency:tree
ã³ãã³ãã§äŸåé¢ä¿ããªãŒã確èªãã競åãçºçããŠããå Žå㯠<dependencyManagement>
ã <exclusions>
ã䜿ã£ãŠè§£æ±ºããŸãã
äŸåé¢ä¿ã¹ã³ãŒã (Dependency Scope)
<scope>
ã¿ã°ã¯ãäŸåé¢ä¿ãã©ã®ãã«ããã§ãŒãºïŒã³ã³ãã€ã«ããã¹ããå®è¡æãªã©ïŒã§å¿
èŠãšããããããŸãæšç§»çäŸåé¢ä¿ã«å«ãããã©ãããæå®ããŸããäž»ãªã¹ã³ãŒãã«ã¯ä»¥äžã®ãã®ããããŸãã
ã¹ã³ãŒã | 説æ | æšç§»çïŒ |
---|---|---|
compile |
ããã©ã«ãã®ã¹ã³ãŒããã³ã³ãã€ã«æããã¹ãæãå®è¡æã®ãã¹ãŠã§ã¯ã©ã¹ãã¹ã«å«ãŸããŸãã | ã¯ã |
provided |
ã³ã³ãã€ã«æãšãã¹ãæã«ã¯å¿ èŠã§ãããå®è¡æã«ã¯JDKãã³ã³ããïŒäŸ: TomcatïŒã«ãã£ãŠæäŸãããããšãæåŸ ããŸããææç©ïŒJAR/WARïŒã«ã¯å«ãŸããŸãããïŒäŸ: Servlet APIïŒ | ããã |
runtime |
å®è¡æãšãã¹ãæã«ã¯å¿ èŠã§ãããã³ã³ãã€ã«æã«ã¯äžèŠã§ããïŒäŸ: JDBCãã©ã€ãïŒ | ã¯ã |
test |
ãã¹ãã³ãŒãã®ã³ã³ãã€ã«æãšãã¹ãå®è¡æã«ã®ã¿å¿ èŠã§ããéåžžã®ã³ã³ãã€ã«ãå®è¡æã«ã¯å«ãŸããŸãããïŒäŸ: JUnit, MockitoïŒ | ããã |
system |
provided ã«äŒŒãŠããŸãããæ瀺çã«ããŒã«ã«ã·ã¹ãã ã®JARãã¡ã€ã«ãæå®ããå¿
èŠããããŸãããªããžããªããã¯è§£æ±ºãããŸãããããŒã¿ããªãã£ãäœããªããããéåžžã¯äœ¿çšãé¿ããã¹ãã§ãã<systemPath> ã®æå®ãå¿
èŠã§ãã |
ããã |
import |
<dependencyManagement> ã»ã¯ã·ã§ã³ã§ã®ã¿äœ¿çšå¯èœã§ããä»ã®POMãã¡ã€ã«ïŒéåžžã¯BOM – Bill of MaterialsïŒã§å®çŸ©ãããäŸåé¢ä¿æ
å ±ãã€ã³ããŒãããŸãã |
ããã |
3.4. ãã«ãã©ã€ããµã€ã¯ã« (Build Lifecycle)
Mavenã¯ããããžã§ã¯ãã®ãã«ãããã»ã¹ãã©ã€ããµã€ã¯ã«ãšããæŠå¿µã§æšæºåããŠããŸããã©ã€ããµã€ã¯ã«ã¯ããã§ãŒãº (Phase) ãšåŒã°ããé åºä»ããããã¹ãããã®éãŸãã§ããç¹å®ã®ãã§ãŒãºãå®è¡ãããšããã®ãã§ãŒãºã«è³ããŸã§ã®ãã¹ãŠã®ãã§ãŒãºãé çªã«å®è¡ãããŸãã
Mavenã«ã¯äž»ã«3ã€ã®çµã¿èŸŒã¿ã©ã€ããµã€ã¯ã«ããããŸãã
clean
: ãã«ãã«ãã£ãŠçæããããã¡ã€ã«ïŒéåžžtarget
ãã£ã¬ã¯ããªïŒãåé€ããããã®ã©ã€ããµã€ã¯ã«ãdefault
(ãŸãã¯build
): ãããžã§ã¯ãã®ãã«ãããã¹ããããã±ãŒãžã³ã°ããããã€ãè¡ãäž»èŠãªã©ã€ããµã€ã¯ã«ãsite
: ãããžã§ã¯ãã«é¢ããããã¥ã¡ã³ããã¬ããŒããçæããWebãµã€ããäœæããããã®ã©ã€ããµã€ã¯ã«ã
Defaultã©ã€ããµã€ã¯ã«ã®äž»èŠãªãã§ãŒãº
æããã䜿ããã default
ã©ã€ããµã€ã¯ã«ã«ã¯å€æ°ã®ãã§ãŒãºããããŸãããç¹ã«éèŠãªãã®ã以äžã«ç€ºããŸãããããã¯é çªã«å®è¡ãããŸãã
validate
: ãããžã§ã¯ããæ£ãããå¿ èŠãªæ å ±ããã¹ãŠå©çšå¯èœãæ€èšŒããŸããcompile
: ãããžã§ã¯ãã®ãœãŒã¹ã³ãŒã (src/main/java
) ãã³ã³ãã€ã«ããŸããtest
: ã³ã³ãã€ã«ãããã³ãŒãã«å¯ŸããŠãé©åãªãŠããããã¹ããã¬ãŒã ã¯ãŒã¯ (äŸ: JUnit) ãçšããŠãã¹ã (src/test/java
) ãå®è¡ããŸããpackage
: ã³ã³ãã€ã«ãããã³ãŒããšãªãœãŒã¹ããé åžå¯èœãªåœ¢åŒ (pom.xml
ã®<packaging>
ã§æå®ããã圢åŒãäŸ: JAR, WAR) ã«ããã±ãŒãžåããŸããææç©ã¯target
ãã£ã¬ã¯ããªã«çæãããŸããverify
: ããã±ãŒãžãæå¹ã§ãããå質åºæºãæºãããŠãããã確èªããããã®ãã§ãã¯ïŒäŸ: çµ±åãã¹ãã®çµææ€èšŒïŒãå®è¡ããŸããinstall
: ããã±ãŒãžãããŒã«ã«ãªããžããªã«ã€ã³ã¹ããŒã«ããŸããããã«ãããä»ã®ããŒã«ã«ãããžã§ã¯ãããäŸåé¢ä¿ãšããŠå©çšã§ããããã«ãªããŸããdeploy
: (éåžžã¯CI/CDç°å¢ã§) å®æããããã±ãŒãžããªã¢ãŒããªããžããªã«ã³ããŒããä»ã®éçºè ããããžã§ã¯ããšå ±æã§ããããã«ããŸãã
äŸãã°ãmvn package
ã³ãã³ããå®è¡ãããšãvalidate
, compile
, test
ãçµãŠ package
ãã§ãŒãºãŸã§ãå®è¡ãããŸããåæ§ã« mvn install
ãå®è¡ãããšãpackage
ãŸã§ã®ãã§ãŒãºã«å ã㊠install
ãã§ãŒãºãå®è¡ãããŸãã
Cleanã©ã€ããµã€ã¯ã«ã®ãã§ãŒãº
pre-clean
: ã¯ãªãŒã³ååŠçãclean
: ååã®ãã«ãã§çæããããã¡ã€ã« (target
ãã£ã¬ã¯ããª) ãåé€ããŸããpost-clean
: ã¯ãªãŒã³åŸåŠçã
mvn clean
ãå®è¡ãããšãé垞㯠clean
ãã§ãŒãºãå®è¡ãããŸãã
Siteã©ã€ããµã€ã¯ã«ã®ãã§ãŒãº
pre-site
: ãµã€ãçæååŠçãsite
: ãããžã§ã¯ãã®ãµã€ãããã¥ã¡ã³ã (target/site
) ãçæããŸããpost-site
: ãµã€ãçæåŸåŠçãsite-deploy
: çæããããµã€ããæå®ãããWebãµãŒããŒã«ãããã€ããŸãã
mvn site
ãå®è¡ãããšãsite
ãã§ãŒãºãŸã§ãå®è¡ãããŸãã
3.5. ãã©ã°ã€ã³ (Plugins) ãš ãŽãŒã« (Goals)
Mavenã®å®éã®åŠçã¯ããã¹ãŠãã©ã°ã€ã³ (Plugin) ã«ãã£ãŠå®è¡ãããŸããMavenã³ã¢èªäœã¯ã©ã€ããµã€ã¯ã«ç®¡çãäŸåé¢ä¿è§£æ±ºãªã©ã®åºæ¬çãªãã¬ãŒã ã¯ãŒã¯ãæäŸããã ãã§ãã³ã³ãã€ã«ããã¹ããããã±ãŒãžã³ã°ãšãã£ãå ·äœçãªã¿ã¹ã¯ã¯ãããããã®ãã©ã°ã€ã³ãæ åœããŸãã
åãã©ã°ã€ã³ã¯ãäžã€ä»¥äžã®ãŽãŒã« (Goal) ãæã£ãŠããŸãããŽãŒã«ã¯ãç¹å®ã®ã¿ã¹ã¯ãå®è¡ããåäœã§ãïŒäŸ: ã³ãŒããã³ã³ãã€ã«ãããŽãŒã«ãJARãã¡ã€ã«ãäœæãããŽãŒã«ïŒã
Mavenã®ã©ã€ããµã€ã¯ã«ã®åãã§ãŒãºã«ã¯ãããã©ã«ãã§ç¹å®ã®ãã©ã°ã€ã³ã®ãŽãŒã«ãçŽä»ããããŠããŸã (ãã€ã³ãã£ã³ã°)ãäŸãã°ãcompile
ãã§ãŒãºã«ã¯ maven-compiler-plugin
ã® compile
ãŽãŒã«ããtest
ãã§ãŒãºã«ã¯ maven-surefire-plugin
ã® test
ãŽãŒã«ããpackage
ãã§ãŒãº (packagingãjarã®å Žå) ã«ã¯ maven-jar-plugin
ã® jar
ãŽãŒã«ãçŽä»ããããŠããŸãã
mvn <phase>
(äŸ: mvn package
) ãå®è¡ãããšãMavenã¯ãã®ãã§ãŒãºããã³ãã以åã®ãã¹ãŠã®ãã§ãŒãºãå®è¡ããåãã§ãŒãºã«çŽä»ãããããŽãŒã«ãé 次å®è¡ãããŸãã
ãŸããã©ã€ããµã€ã¯ã«ãã§ãŒãºãšã¯ç¬ç«ããŠãç¹å®ã®ãã©ã°ã€ã³ã®ãŽãŒã«ãçŽæ¥å®è¡ããããšãå¯èœã§ããã³ãã³ã㯠mvn <plugin-prefix>:<goal>
(ãŸã㯠mvn <groupId>:<artifactId>[:<version>]:<goal>
) ã®åœ¢åŒã«ãªããŸãã
äŸ:
mvn compiler:compile
:maven-compiler-plugin
ã®compile
ãŽãŒã«ã®ã¿ãå®è¡ããŸãïŒã©ã€ããµã€ã¯ã«ã®ä»ã®ãã§ãŒãºã¯å®è¡ãããŸããïŒãmvn dependency:tree
:maven-dependency-plugin
ã®tree
ãŽãŒã«ãå®è¡ããäŸåé¢ä¿ããªãŒã衚瀺ããŸãã
pom.xml
ã® <build><plugins>
ã»ã¯ã·ã§ã³ã§ã䜿çšãããã©ã°ã€ã³ã®ããŒãžã§ã³ãæå®ããããããã©ã«ãã®åäœãã«ã¹ã¿ãã€ãºããããç¹å®ã®ãã§ãŒãºã«è¿œå ã®ãŽãŒã«ãçŽä»ãããããããšãã§ããŸãããã©ã°ã€ã³ã®ããŒãžã§ã³ãæ瀺çã«æå®ããããšã¯ããã«ãã®åçŸæ§ãä¿ã€ããã«éåžžã«éèŠã§ãã
代衚çãªMavenãã©ã°ã€ã³:
ãã©ã°ã€ã³ (artifactId) | äž»ãªãŽãŒã« | 説æ |
---|---|---|
maven-clean-plugin | clean | target ãã£ã¬ã¯ããªãåé€ãã (clean ãã§ãŒãºã«ãã€ã³ã) |
maven-compiler-plugin | compile , testCompile | ãœãŒã¹ã³ãŒããšãã¹ãã³ãŒããã³ã³ãã€ã«ãã (compile , test-compile ãã§ãŒãºã«ãã€ã³ã) |
maven-surefire-plugin | test | ãŠããããã¹ããå®è¡ãã (test ãã§ãŒãºã«ãã€ã³ã) |
maven-jar-plugin | jar | JARãã¡ã€ã«ãçæãã (package ãã§ãŒãºã«ãã€ã³ã) |
maven-war-plugin | war | WARãã¡ã€ã«ãçæãã (package ãã§ãŒãºã«ãã€ã³ã) |
maven-install-plugin | install | ææç©ãããŒã«ã«ãªããžããªã«ã€ã³ã¹ããŒã«ãã (install ãã§ãŒãºã«ãã€ã³ã) |
maven-deploy-plugin | deploy | ææç©ããªã¢ãŒããªããžããªã«ãããã€ãã (deploy ãã§ãŒãºã«ãã€ã³ã) |
maven-resources-plugin | resources , testResources | ãªãœãŒã¹ãã¡ã€ã« (src/main/resources , src/test/resources ) ãåŠçã»ã³ããŒãã (process-resources , process-test-resources ãã§ãŒãºã«ãã€ã³ã) |
maven-site-plugin | site , deploy | ãããžã§ã¯ããµã€ããçæã»ãããã€ãã (site , site-deploy ãã§ãŒãºã«ãã€ã³ã) |
maven-dependency-plugin | tree , analyze , etc. | äŸåé¢ä¿ã®ããªãŒè¡šç€ºãåæãªã©ãè¡ã (ç¹å®ã®ãã§ãŒãºã«ã¯éåžžãã€ã³ããããªã) |
maven-enforcer-plugin | enforce | ç°å¢ïŒMavenããŒãžã§ã³ãJDKããŒãžã§ã³ãªã©ïŒãäŸåé¢ä¿ã«é¢ããã«ãŒã«ã匷å¶ãã (validate ãã§ãŒãºã«ãã€ã³ãããããšãå€ã) |
4. åºæ¬çãªMavenã³ãã³ã âšïž
Mavenã®æäœã¯ãäž»ã«ã³ãã³ãã©ã€ã³ãã mvn
ã³ãã³ãã䜿ã£ãŠè¡ããŸãã以äžã«ãã䜿ãããã³ãã³ãïŒã©ã€ããµã€ã¯ã«ãã§ãŒãºã®å®è¡ïŒã瀺ããŸãã
ã³ãã³ã | 説æ | å®è¡ãããäž»ãªãã§ãŒãº |
---|---|---|
mvn clean |
ãã«ãææç© (target ãã£ã¬ã¯ããª) ãåé€ããŸãã |
clean |
mvn compile |
ãããžã§ã¯ãã®ãœãŒã¹ã³ãŒããã³ã³ãã€ã«ããŸããçµæ㯠target/classes ã«åºåãããŸãã |
validate , …, compile |
mvn test |
ãŠããããã¹ããå®è¡ããŸãããã¹ãçµæ㯠target/surefire-reports ã«åºåãããŸãã |
validate , …, compile , …, test-compile , test |
mvn package |
ã³ã³ãã€ã«ããã¹ããå®è¡ãããããžã§ã¯ããããã±ãŒãžåïŒäŸ: JAR, WARïŒããŸããææç©ã¯ target ãã£ã¬ã¯ããªã«çæãããŸãã |
validate , …, test , …, package |
mvn verify |
ããã±ãŒãžã³ã°åŸãçµ±åãã¹ãã®å®è¡ãå質ãã§ãã¯ãªã©ãè¡ããŸãã | validate , …, package , …, verify |
mvn install |
ããã±ãŒãžã³ã°åŸãææç©ãããŒã«ã«ãªããžã㪠(~/.m2/repository ) ã«ã€ã³ã¹ããŒã«ããŸãã |
validate , …, package , …, verify , install |
mvn deploy |
ããŒã«ã«ãªããžããªãžã®ã€ã³ã¹ããŒã«åŸãææç©ããªã¢ãŒããªããžããªã«ãããã€ããŸã (pom.xml ã«ãããã€å
ã®èšå®ãå¿
èŠ)ã |
validate , …, install , deploy |
mvn site |
ãããžã§ã¯ãã®ãµã€ãããã¥ã¡ã³ããçæããŸããçµæ㯠target/site ã«åºåãããŸãã |
(Siteã©ã€ããµã€ã¯ã«) pre-site , site |
mvn clean install |
clean ã©ã€ããµã€ã¯ã«ãå®è¡ããŠãã install ãã§ãŒãºãŸã§ãå®è¡ããŸãããã䜿ãããçµã¿åããã§ãã |
clean â validate , …, install |
ç¹å®ã®ãã©ã°ã€ã³ãŽãŒã«ãå®è¡ããã³ãã³ãããããŸãã
ã³ãã³ã | 説æ |
---|---|
mvn dependency:tree |
ãããžã§ã¯ãã®äŸåé¢ä¿ããªãŒã衚瀺ããŸããæšç§»çäŸåé¢ä¿ãããŒãžã§ã³ã®ç«¶åã確èªããã®ã«åœ¹ç«ã¡ãŸãã |
mvn dependency:analyze |
äŸåé¢ä¿ãåæããæªäœ¿çšã®äŸåé¢ä¿ããpom.xml ã«çŽæ¥å®£èšãããŠããªãã䜿çšãããŠããäŸåé¢ä¿ãªã©ãæ€åºããŸãã |
mvn help:effective-pom |
芪POMãèšå®ãã¡ã€ã« (settings.xml ) ãªã©ããç¶æ¿ã»é©çšãããæçµçãªPOMã®å
容ã衚瀺ããŸãã |
mvn versions:display-dependency-updates |
(versions-maven-plugin ãå¿
èŠ) 䜿çšããŠããäŸåé¢ä¿ã«æ°ããããŒãžã§ã³ããªãããã§ãã¯ããŸãã |
5. ç°¡åãªãããžã§ã¯ãäœæäŸ ðïž
Mavenã«ã¯ãArchetype (ã¢ãŒãã¿ã€ã) ãšãããããžã§ã¯ãã®é圢ãäœæããæ©èœããããŸããããã䜿ããšãæšæºçãªãã£ã¬ã¯ããªæ§é ãåºæ¬ç㪠pom.xml
ãæã€ãããžã§ã¯ãã¹ã±ã«ãã³ãç°¡åã«çæã§ããŸãã
ããã§ã¯ãæãåºæ¬çãªJavaã¢ããªã±ãŒã·ã§ã³ã®é圢 (maven-archetype-quickstart
) ã䜿ã£ãŠãããžã§ã¯ããäœæããŠã¿ãŸãããã
-
ã³ãã³ãã®å®è¡: ã³ãã³ãããã³ãããŸãã¯ã¿ãŒããã«ãéãããããžã§ã¯ããäœæããããã£ã¬ã¯ããªã«ç§»åããŠã以äžã®ã³ãã³ããå®è¡ããŸãã
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
-DgroupId=com.example
: äœæãããããžã§ã¯ãã®ã°ã«ãŒãIDãæå®ããŸãã-DartifactId=my-app
: äœæãããããžã§ã¯ãã®ã¢ãŒãã£ãã¡ã¯ãIDãæå®ããŸããããããããžã§ã¯ãã®ãã£ã¬ã¯ããªåã«ããªããŸãã-DarchetypeArtifactId=maven-archetype-quickstart
: 䜿çšããã¢ãŒãã¿ã€ãã®ã¢ãŒãã£ãã¡ã¯ãIDãæå®ããŸãã-DarchetypeVersion=1.4
: 䜿çšããã¢ãŒãã¿ã€ãã®ããŒãžã§ã³ãæå®ããŸãã-DinteractiveMode=false
: 察話ã¢ãŒããç¡å¹ã«ãããã¹ãŠã®æ å ±ãã³ãã³ãã©ã€ã³åŒæ°ã§æå®ããŸãããããçç¥ãããšã察話圢åŒã§æ å ±ãå ¥åã§ããŸãã
-
çæçµæã®ç¢ºèª: ã³ãã³ããæåãããšãã«ã¬ã³ããã£ã¬ã¯ããªã«
my-app
ãšããååã®ãã£ã¬ã¯ããªãäœæãããŸãããã®äžèº«ã¯ä»¥äžã®ããã«ãªã£ãŠããã¯ãã§ããmy-app/ âââ pom.xml # ãããžã§ã¯ãã®POMãã¡ã€ã« âââ src/ âââ main/ â âââ java/ â âââ com/ â âââ example/ â âââ App.java # ãµã³ãã«ã®ã¡ã€ã³ã¯ã©ã¹ âââ test/ âââ java/ âââ com/ âââ example/ âââ AppTest.java # ãµã³ãã«ã®ãã¹ãã¯ã©ã¹
pom.xml
ã«ã¯åºæ¬çãªèšå®ãèšè¿°ãããŠãããApp.java
ã«ã¯ç°¡å㪠“Hello World!” ããã°ã©ã ãAppTest.java
ã«ã¯JUnitã䜿ã£ãç°¡åãªãã¹ãã³ãŒããå«ãŸããŠããŸãã -
ãã«ããšå®è¡: äœæããããããžã§ã¯ããã£ã¬ã¯ããªã«ç§»åããMavenã³ãã³ããè©ŠããŠã¿ãŸãããã
cd my-app mvn package
ããã«ãããã³ã³ãã€ã«ããã¹ããããã±ãŒãžã³ã°ãå®è¡ããã
target
ãã£ã¬ã¯ããªå ã«my-app-1.0-SNAPSHOT.jar
ã®ãããªJARãã¡ã€ã«ãçæãããŸããçæãããJARãã¡ã€ã«ãå®è¡ããã«ã¯ã次ã®ããã«ããŸãã
java -cp target/my-app-1.0-SNAPSHOT.jar com.example.App
ã³ã³ãœãŒã«ã« “Hello World!” ãšè¡šç€ºãããã¯ãã§ãã
ãã®ããã«ãArchetypeã䜿ãã°ãæšæºçãªæ§é ãæã€Mavenãããžã§ã¯ããçŽ æ©ãç«ã¡äžããããšãã§ããŸããWebã¢ããªã±ãŒã·ã§ã³çšã® maven-archetype-webapp
ãªã©ãæ§ã
ãªçš®é¡ã®ArchetypeãçšæãããŠããŸãã
6. ãŸãšã â
Apache Mavenã¯ãJavaéçºã«ããããã«ãã®èªååãšäŸåé¢ä¿ç®¡çãå¹çåããããã®åŒ·åãªããŒã«ã§ãããã®äž»ãªã¡ãªãããå確èªããŸãããã
- ãã«ãããã»ã¹ã®æšæºåã»èªåå:
mvn
ã³ãã³ãäžã€ã§ã³ã³ãã€ã«ãããããã€ãŸã§ã®äžé£ã®ããã»ã¹ãå®è¡ã§ããéçºå¹çãåäžããŸãã - ç°¡åãªäŸåé¢ä¿ç®¡ç:
pom.xml
ã«èšè¿°ããã ãã§ãå¿ èŠãªã©ã€ãã©ãªãèªåã§ããŠã³ããŒãã»ç®¡çããŠãããŸããæšç§»çäŸåé¢ä¿ã解決ããããããæäœæ¥ã§ã®ã©ã€ãã©ãªç®¡çã®æéãå€§å¹ ã«åæžãããŸãã - ãããžã§ã¯ãæ§é ã®æšæºå: èŠçŽã«åºã¥ãããã£ã¬ã¯ããªæ§é ã«ããããããžã§ã¯ãã®èŠéããè¯ããªããããŒã éçºã容æã«ãªããŸãã
- è±å¯ãªãã©ã°ã€ã³ãšã³ã·ã¹ãã : æ§ã ãªãã©ã°ã€ã³ãå©çšããããšã§ããã«ãããã»ã¹ã®æ¡åŒµãã«ã¹ã¿ãã€ãºã容æã«è¡ããŸãã
- æ
å ±ã®éçŽ: ãããžã§ã¯ãã«é¢ããæ
å ±ã
pom.xml
ã«éçŽãããããã管çããããããªããŸãã
Mavenã䜿ãããªãããšã§ãJavaãããžã§ã¯ãã®éçºããã»ã¹ãããã¹ã ãŒãºã«ãããå¹ççã«é²ããããšãã§ããŸããæåã¯èŠããããšãå€ããããããŸããããåºæ¬çãªæŠå¿µãšã³ãã³ããç解ããã°ããã®æ©æµãååã«åããããšãã§ããã§ãããã
次ã®ã¹ããããšããŠã¯ããã«ãã¢ãžã¥ãŒã«ãããžã§ã¯ãã®ç®¡çããããã¡ã€ã«ã䜿ã£ãç°å¢ããšã®èšå®åãæ¿ããã«ã¹ã¿ã ãã©ã°ã€ã³ã®éçºãªã©ã«ææŠããŠã¿ãã®ãè¯ãã§ããããHappy Maving! ð¥³
ã³ã¡ã³ã