在用第三方的jar時,之前很笨在.m2的repository目錄下手動建置groupId子目錄,其實有現成的語法可以把local的jar檔更新至repository。語法如下:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> |
這樣在mvn package或mvn install時就不會找不到3rd的jar而無法打包。接著如何設定Jar的MANIFEST.MF,指定Main Class以及把reference的jar全部copy出來。可參考如下pom.xml設定:
<project> ... <build> <finalName>Foo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <configuration> <archive> <manifest> <mainClass>com.foo.bar.MainClass</mainClass> <!-- 指定MANIFEST.MF的Main Class --> <classpathPrefix>lib/</classpathPrefix> <!-- 指定MANIFEST.MF的Class Path的前置 --> <addClasspath>true</addClasspath> <!-- 指定MANIFEST.MF的Class Path --> </manifest> <manifestEntries> <mode>development</mode> <url>${pom.url}</url> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- Copy相依Jar檔至/target目錄下的lib目錄,也與上述的classpathPrefix標籤相符 --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project> |
留言列表