2013年9月16日月曜日

小ネタ mavenを使ってjava -cp を実行しよう

java -cp たまに使うけどライブラリを探したりするの面倒
ライブラリとか全部mavenに任せようぜという人向け

下のようなコマンドを実行しろとかたまにある
java -cp lib/jetty-util-xxx.jar org.eclipse.jetty.util.security.Password password
 
 
こういうのはpom.xmlで下みたいに指定して
つぎのコマンドを実行すればOK

mvn exec:java -Dexec.mainClass="org.eclipse.jetty.util.security.Password" -Dexec.args="password" --quiet  



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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.gauuud.passgen</groupId>
 <artifactId>passgen</artifactId>
 <version>0.0.1</version>
 <dependencies>
  <dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-util</artifactId>
   <version>8.1.12.v20130726</version>
  </dependency>
 </dependencies>
</project>
 
 
または、mainClassをpomで指定する
mvn exec:java -Dexec.args="password" --quiet  

<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.gauuud.passgen</groupId>
 <artifactId>passgen</artifactId>
 <version>0.0.1</version>
 <dependencies>
  <dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-util</artifactId>
   <version>8.1.12.v20130726</version>
  </dependency>
 </dependencies>
  <build>
    <plugins>
      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>              
        <configuration>
          <mainClass>org.eclipse.jetty.util.security.Password</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
 </project>
 
 
または、mainClassを指定してjar化する 
mvn packageしてtarget以下のjar-with-dependenciesを実行する
 
java -jar passgen-0.0.1-jar-with-dependencies.jar password 


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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.gauuud.passgen</groupId>
 <artifactId>passgen</artifactId>
 <version>0.0.1</version>
 <dependencies>
  <dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-util</artifactId>
   <version>8.1.12.v20130726</version>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <mainClass>org.eclipse.jetty.util.security.Password</mainClass>
       <packageName>passgen</packageName>
       <addClasspath>true</addClasspath>
       <addExtensions>true</addExtensions>
       <classpathPrefix>dependency</classpathPrefix>
      </manifest>
     </archive>
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
     <archive>
      <manifest>
       <mainClass>org.eclipse.jetty.util.security.Password</mainClass>
      </manifest>
     </archive>

    </configuration>
    <executions>
     <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- append to the packaging phase. -->
      <goals>
       <goal>single</goal> <!-- goals == mojos -->
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
</project>





 
 
あまり効率的とは言えない?
プライベートリポジトリに登録して使い回せばいいじゃないか
nexusよいですよ 

0 件のコメント: