Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Thursday, March 19, 2009

excluding transitive dependency in maven

ใครที่เคยใช้ maven คงจะรู้ดีกว่า มันช่วยลดอาการปวดหัวในการจัดการกับ jar file ได้ระดับหนึ่ง เพราะมันช่วยเราจัดการพวก transitive dependency ให้เราได้ (แต่ไปปวดหัวเรื่อง setup มันแทน)

แต่การที่มัน solve transitive dependency ให้ก็นำมาซึ่งปัญหาอันใหม่ นั่นก็คือ version conflict, สมมติว่า project เรามี dependency ถึง library Foo กับ library Bar. ทั้ง Foo และ Bar บังเอิญมี dependency ซึ่ง Library Z เหมือนๆกัน แต่ดันเป็นคนละ version. เมื่อเรา build project, เราก็จะมี library Z เข้ามาใน class path เราทั้งสอง version, ทำให้เกิด error พวก missing method หรือ missing class ได้
(Note: ที่ผมเจอ ผมเจอปัญหานี้ตอน mvn eclipse:eclipse, แต่จากการทดสอบด้วยคำสั่ง mvn dependency:resolve ปรากฎว่า มันได้ผลลัพท์ไม่เหมือนกัน เจ้า dependency:resolve ดูเหมือนจะจัดการได้ถูกต้อง)

วิธีแก้ไขก็คือ การระบุ exclusion ลงใน pom.xml ของ maven
จากของเดิม
<dependency>
<groupId>my.pphetra</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>my.pphetra</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
</dependency>

ไปเป็น
<dependency>
<groupId>my.pphetra</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>test</groupId>
<artifactId>Z</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>my.pphetra</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
</dependency>


ทางแก้ดูแล้วง่ายดาย แต่จริงๆแล้ว มันไปยุ่งตอนที่หาว่า library ตัวไหนที่เราควรจะไปใส่ excludes ให้มัน
วิธีการก็คือ ตอนที่สั่ง mvn command (กรณีของผมก็คือ eclipse:eclipse) ให้ใส่ -X ลงไปด้วย
จากนั้นก็นั่งไล่หาว่า เจ้าตัวไหนคือตัวปัญหา (คำเตือน วิธีนี้มีผลข้างเคียงก็คือ ทำให้เกิดอาการง่วงนอน)

Related link from Roti

Tuesday, December 11, 2007

native dependency with maven

วันนี้มีโจทย์ที่ต้อง config maven ให้เลือกใช้ dependency แบบที่ต้องขึ้นอยู่กับ platform
ต้นตอของเรื่องก็คือเจ้า swt library ที่ต้องเลือก jar ให้ถูกกับเครื่องที่จะไป run
เช่นถ้า build บน linux ก็ควรจะใช้ swt-linux-gtk-native.jar
หรือถ้าบน windows ก็ต้องเป็น swt-win32-native.jar

เริ่มต้นด้วยการ setup repository ก่อน
หน้าตาของ repository ก็ประมาณนี้

+ swt
+ swt-linux-gtk-native
+ 3.3.0
- swt-linux-gtk-native-3.3.0.jar
+ swt-win32-native
+ 3.3.0
- swt-win32-native-3.3.0.jar


จากนั้นก็ให้กำหนด profiles ใน pom.xml
สังเกตุว่าใน profile จะมี activate block ที่ใช้ข้อมูล os เป็น condition ในการ activate
<profiles>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<swt.os-dep>swt-linux-gtk-native</swt.os-dep>
<swt.version>3.3.0</swt.version>
</properties>
</profile>
<profile>
<id>win32</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<swt.os-dep>swt-win32-native</swt.os-dep>
<swt.version>3.3.0</swt.version>
</properties>
</profile>
</profiles>


จากนั้นใน dependency list ของเรา ก็ให้อ้างถึง jar โดยใช้ property name แทน

<dependency>
<groupId>swt</groupId>
<artifactId>${swt.os-dep}</artifactId>
<version>${swt.version}</version>
</dependency>

Related link from Roti

Wednesday, July 18, 2007

Build flex with Maven

ที่บริษัทฯใช้ maven ในการ build project และควบคุม dependency
ตอนนี้เริ่มมี flex component เข้ามาปะปนใน java project ด้วย
ก็เลยอยากให้ process กับ project structure ที่ใช้พัฒนา flex
เป็นมาตรฐานเดียวกัน

search ดูใน google พบว่ามีคนทำ plugin อยู่ 2 ราย
แต่รายที่ลองแล้ว work ก็คือ
maven-flex2-plugin

เป้าหมายของ maven-flex2-plugin ก็คือ
การช่วย build และ สร้าง package swc (flex library) หรือ swf (flex application) ให้เรา
การใช้งาน ก็เริ่มด้วยการสร้าง pom.xml
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>cnug</groupId>
<!-- ชื่อของ artifact -->
<artifactId>hangman</artifactId>
<version>1.0</version>
<!-- ชนิดของ package -->
<packaging>swf</packaging>
<properties>
<flex.home>/home/pphetra/dev/flex</flex.home>
</properties>
<!-- PLUGIN REPOSITORIES -->
<pluginRepositories>

<pluginRepository>
<id>central</id>
<name>Central Maven Repository</name>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>

<pluginRepository>
<id>maven-snapshots</id>
<name>Maven Snapshots Repository</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
</pluginRepository>

<pluginRepository>
<id>israfil</id>
<name>israfil Repository</name>
<url>http://www.israfil.net/maven2</url>
</pluginRepository>

</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>net.israfil.mojo</groupId>
<artifactId>maven-flex2-plugin</artifactId>
<version>1.0</version>
<extensions>true</extensions>
<configuration>
<useNetwork>true</useNetwork>
<strict>true</strict>
<warning>true</warning>
<!-- application mxml file -->
<main>main.mxml</main>
</configuration>
</plugin>
</plugins>
</build>
</project>


ส่วน project structure, เราจะวาง code ของ flex ไว้ใน
src/main/flex

ตัวอย่าง application hangman ที่ผมลองสร้างดู
ผมวาง code ไว้ใน directory แบบนี้

+ src
+ main
+ flex
- main.mxml
+ assets
- win.png
- lose.png
- mistake0.png
...
+ hangman
+ core
- Game.as
+ ui
- GameGraphics.as
- Unreveal.as
- Letters.as

Related link from Roti