středa 3. června 2009

FatJar s maven assembly pro Spring aplikaci

Jar file v Jave je dobra vec na delivery jednoducheho programu, ktery nezavisi na zadnych knihovnach. Jakmile pouzijeme vic knihoven, musime uvest do parametru --classpath vsechny jary, na kterych projekt zavisi. Pro jednou se to da zkousnout, ale resit to pokazde ? :) Nastesti existuje reseni - jmenuje se Fat Jar. Ten jde vytvorit napr. z Eclipsu pomoci tohoto pluginu. Je tu nekdo, kdo jeste pouziva ant? Tomu nasledujici clanek moc neprinese. Pro ty z Vas, kdo pouzivaji pro build a dependency managment maven, mam ukazku trochu slozitejsiho nastaveni maven assembly pluginu. Jedna se o plugin, ktery dokaze vzit zdrojovy kod a ve fazi buildeni (presneji v package goalu) rozjede vytvareni distribuce pro aplikaci. Je to takova nadstavba nad maven-jar/war/... pluginem. Tady ukazu, jak jsem vybuildil Java SE aplikaci se Springem (Hibernatem atd.). Vyskytli se 2 problemy.
  1. Je potreba pouzit posledni verzi (tj. 2.2-beta-3). Predchozi verze mela bug, ze misto toho, aby se pouzil jen jeden soubor stejneho jmena v classpath se konkatenovaly soubory do sebe.
  2. Unable to locate Spring NamespaceHandler for XML schema namespace, s posledni verzi pluginu proces reseni duplicit funguje tak, ze vyhraje prvni (resp. ta z projektu, ktery buildime). Spring potrebuje v META-INF mit soubory spring.handlers a spring-schemas. Je treba projit obsah vsech spring-* zavislosti a rucne konkatenovat obsah do sveho souboru v META-INF buildeneho projektu.
edit: Unable to locate schema namespace resi lepe maven shade plugin, viz http://maestro-lab.blogspot.com/2010/06/upgrading-to-spring-3-unable-to-locate.html Co je potreba tedy nastavit? V prvni rade pom.xml!
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <build>
     <plugins>
         <plugin>
             <artifactid>
                 maven-assembly-plugin
             </artifactid>
             <!-- verze je tu nutna, 2.2-beta-2 ma bug, kde se misto prepsani konkatenuji soubory se stejnym jmenem -->
             <version>
                 2.2-beta-3
             </version>
             <executions>
                 <execution>
                     <id>
                         make-assembly
                     </id>
                     <phase>
                         package
                     </phase>
                     <goals>
                         <goal>
                             assembly
                         </goal>
                     </goals>
                     <configuration>
                         <appendassemblyid>
                             false
                         </appendassemblyid>
                         <!-- ve vetsine pripadu staci uvest
    <descriptorrefs>
     <descriptorref>jar-with-dependencies</descriptorref>
    </descriptorrefs>
   -->
                         <descriptors>
                             <descriptor>
                                 ${basedir}/src/assemble/fatJar.xml
                             </descriptor>
                         </descriptors>
                         <archive>
                             <manifest>
                                 <mainclass>
                                     your.package.name.Main
                                 </mainclass>
                             </manifest>
                         </archive>
                     </configuration>
                 </execution>
             </executions>
         </plugin>
     </plugins>
     <!-- potrebujeme spojit spring.handlers a spring.schemas -->
     <resources>
         <resource>
             <directory>
                 src/main/META-INF
             </directory>
             <targetpath>
                 META-INF
             </targetpath>
         </resource>
     </resources>
 </build>
</project>

Pokud nepotrebujeme nic excludovat, includovat, staci pouzit zmineny descriptor jar-with-dependencies. Pokud ano, je treba jej poupravit a vytvorit spec. assembly skript. V tomto pripade /src/assembly/fatJar.xml.
<assembly schemalocation="http://maven.apache.org/xsd/assembly-1.0.0.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance">
 <id>
     fatJar
 </id>
 <formats>
     <format>
         jar
     </format>
 </formats>
 <includebasedirectory>
     false
 </includebasedirectory>
 <dependencysets>
     <dependencyset>
         <outputdirectory>
         </outputdirectory>
         <outputfilenamemapping>
         </outputfilenamemapping>
         <unpack>
             true
         </unpack>
         <scope>
             runtime
         </scope>
     </dependencyset>
 </dependencysets>
 <filesets>
     <fileset>
         <directory>
             target/classes
         </directory>
         <outputdirectory>
         </outputdirectory>
         <!-- must exclude by hand content of src/main/release, there is no anything like fileSet priority -->
         <excludes>
             <exclude>
                 database.properties
             </exclude>
             <exclude>
                 log4j.xml
             </exclude>
         </excludes>
     </fileset>
     <fileset>
         <directory>
             src/main/release
         </directory>
         <outputdirectory>
         </outputdirectory>
     </fileset>
 </filesets>
</assembly>

Pro ty kdo budete chtit vybuildit Spring aplikaci, pridavam jeste workaround pro pred chvili zminene soubory umistene v src/main/META-INF. spring.handlers
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler

http\://www.springframework.org/schema/security=org.springframework.security.config.SecurityNamespaceHandler

http\://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler

http\://www.springframework.org/schema/webflow-config=org.springframework.webflow.config.WebFlowConfigNamespaceHandler
a spring.schemas
http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd

http\://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-2.0.4.xsd
http\://www.springframework.org/schema/security/spring-security-2.0.xsd=org/springframework/security/config/spring-security-2.0.xsd
http\://www.springframework.org/schema/security/spring-security-2.0.1.xsd=org/springframework/security/config/spring-security-2.0.1.xsd
http\://www.springframework.org/schema/security/spring-security-2.0.2.xsd=org/springframework/security/config/spring-security-2.0.2.xsd
http\://www.springframework.org/schema/security/spring-security-2.0.4.xsd=org/springframework/security/config/spring-security-2.0.4.xsd

http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd

http\://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd=org/springframework/webflow/config/spring-webflow-config-1.0.xsd

http\://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd=org/springframework/webflow/config/spring-webflow-config-2.0.xsd
A fat jar je na svete :) :D

Žádné komentáře: