トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

maven

mavenを使ったHelloWorld

プロジェクト作成

mvn archetype:generate -DgroupId=com.example -DartifactId=sample
groupId
ルートパッケージに使用される
artifactId
プロジェクト名。ディレクトリ名などに使用される

ビルド

mvn compile
プログラムをコンパイル
mvn package
jarファイル作成
mvn package -DskipTests=true
テストをスキップしてjarファイル作成
mvn dependency
build-classpath:クラスパスを表示。コマンドラインで手動で実行したいときとか便利。
mvn install dependency
copy-dependencies :必要なjarファイルをtargetのdependencyにダウンロードする

実行

java -cp target xxxxx.jar com.example.App

実行(exec-maven-pluginを使う)

pom.xmlのpluginsに以下を追記。

       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <version>1.2.1</version>
         <configuration>
           <mainClass>com.example.App</mainClass>
         </configuration>
       </plugin>

mainClassはメインクラス。

mvn exec:java

使い方いろいろ

 コンパイルの対象のファイルを減らす

標準のままだと全てのJavaファイルがコンパイルの対象となる。
変更のあったファイル(classファイルより新しいJavaファイルのみ)をコンパイルするほうが効率が良い
pom.xmlのpluginsに以下を追加すると新しいファイルのみコンパイルの対象となる。

 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.1</version>
   <configuration>
     <useIncrementalCompilation>false</useIncrementalCompilation>
   </configuration>
 </plugin>

 ソースパス

     <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>build-helper-maven-plugin</artifactId>
       <executions>
         <execution>
           <id>add-source</id>
           <phase>generate-sources</phase>
           <goals>
             <goal>add-source</goal>
           </goals>
           <configuration>
             <sources>
               <source>../demo2/src/main/java</source>
             </sources>
           </configuration>
         </execution>
       </executions>
     </plugin>

 一部ソース除外

ソースパスを追加した場合などで、不要なファイルがある場合は除外できる。
ただし、指定方法は絶対パス等ではなく、

<exclude>**/some/full/directory/*</exclude>
<exclude>**/some/single/File.java</exclude>

などのような指定方法をする。

     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <configuration>
         <excludes>
           <exclude>**/demo2/Demo2Application.java</exclude>
         </excludes>
       </configuration>
     </plugin>

 ローカルリポジトリ(.m2)のパスを変更

環境変数で変更する方法

https://stackoverflow.com/questions/16591080/maven-alternative-m2-directory

export MAVEN_OPTS="-Dmaven.repo.local=/path/to/repository"

[カテゴリ: プログラミング言語 > Java]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2023年08月17日 20時22分54秒