Tuesday, October 14, 2008

Maven2, Asyik.... cobain yu...

Bismillah.....
Kembali lagi ke Maven ya.... di posting sebelumnya baru dibahas tentang instalasi Maven. Nah sekarang klo ada yang mo nyoba gmana kerja dan fungsinya, yu kita nyoba, yu........

Sebelum mulai, klo pernah nyoba building tool mungkin akrab dengan ant. Maven gk beda jauh sih. Tapi kata para developer Maven lebih mantaaap.... salah satu kebolehanya kayak gini, library yang kita butuhkan gk perlu kita sediakan dulu dikomputer kita, karena Maven yang bakal nyari & download sendiri di internet. Kita cukup tulis/konfigurasi di file pom.xml nya librari yang kita butuhkan. Emang sih butuh konek internet di awalnya, tapi klo udah ada di repositori lokal (librarinya) gk perlu lagi konek internet. Yang lainnya lagi kita bisa bikin paket projek dengan menuliskan satu perintah, kemudian Maven bakal bikin struktur projek kita bahkan download juga librarinya. Nah biar gak bingung kita langsung coba aja yach....


Nih contoh projek yang bisa kita coba. Kita akan bikin projek pake fasilitas archetype. Sebenarnya bisa pake cara lain tapi lain kali dijelasin lagi yach.... sementara kita coba pake cara ini dulu.

Di command line tuliskan ato copy kan perintah berikut ini

mvn archetype:create _DgroupId=com.mycompany.app _DartifactId=my_app

misal:
C:\Users\bhangun>mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=m
y_app

nah hasilnya kayak gini....................

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.mycompany.app
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc
hetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
4K downloaded
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating OldArchetype: maven-archetype-qui
ckstart:RELEASE
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: packageName, Value: com.mycompany.app
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my_app
[INFO] Parameter: basedir, Value: C:\Users\bhangun
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM
***********************
[INFO] OldArchetype created in dir: C:\Users\bhangun\my_app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13 seconds
[INFO] Finished at: Tue Oct 14 08:19:56 ICT 2008
[INFO] Final Memory: 8M/254M
[INFO] ------------------------------------------------------------------------

C:\Users\bhangun>

Catatan: perlu dicatet bahwa dilokasi mana kita menuliskan perintah kayak diatas tadi, maka disitulah Maven bakal bikin paket/folder projek kita. Jadi biar gak pusing nyarinya, pastikan kita berada di direktori ato folder tempat projek-projek kita berada. Inga inga.......!!!! 

Stelah itu coba deh buka foldernya, folder yang mana @#$@#$?.......
Ya folder yang dibikin Maven tadi lah..... diperintah tadi Maven bikinin kita folder my-app sebagai root dari projek kita. Coba liat lagi perintahnya..... bener kan. Hehehe... tnang-tnang. Nah sekarang ketikan:

cd my-app

coba liat strukturnya kayk gini, bener kan:

my_app
|__ pom.xml
`__ src
|__ main
| `__ java
| `__ com
| `__ mycompany
| `__ app
| `__ App.java
`__ test
`__ java
`__ com
`__ mycompany
`__ app
`__ AppTest.java


Lalu dimana source codenya. Dimana hayo........ semuanya ada di dalem folder src/main/java. Nah selanjutanya folder package aja. Nah tadi perintahnya kita sediakan source untuk Test juga jadi dibikin juga deh paket untuk test nya.

Lalu apalagi.......??
Nah peta dan kunci paket kita ada di file pom.xml. coba liat kayak gini kan....

Mungkin lain kali klo ada waktu kita bakal bahas tentang seputar pom.xml nya. Ato mungkin yang lain yang mo jelasin ok...

Selanjutnya kita compile deh paket tadi. Caranya gimana....???
Ketika perintah berikut, tapi ingat ketikannya di dalam folder my_app.

mvn package

contohnya:
C:\Users\bhangun\my_app>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building my_app
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\Users\bhangun\my_app\target\classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to C:\Users\bhangun\my_app\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\Users\bhangun\my_app\target\surefire-report
s

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.12 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-project/2.0.7/
maven-project-2.0.7.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven/2.0.7/maven-2.
0.7.pom
10K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-settings/2.0.7
/maven-settings-2.0.7.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-model/2.0.7/ma
ven-model-2.0.7.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-profile/2.0.7/
maven-profile-2.0.7.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-artifact-manag
er/2.0.7/maven-artifact-manager-2.0.7.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-repository-met
adata/2.0.7/maven-repository-metadata-2.0.7.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-artifact/2.0.7
/maven-artifact-2.0.7.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-plugin-registr
y/2.0.7/maven-plugin-registry-2.0.7.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.3/m
aven-archiver-2.3.pom
3K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-shared-
components/8/maven-shared-components-8.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1
.0-alpha-9/plexus-archiver-1.0-alpha-9.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-components
/1.1.9/plexus-components-1.1.9.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus/1.0.10/ple
xus-1.0.10.pom
8K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-container-
default/1.0-alpha-15/plexus-container-default-1.0-alpha-15.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-containers
/1.0-alpha-15/plexus-containers-1.0-alpha-15.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus/1.0.9/plex
us-1.0.9.pom
7K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-component-
api/1.0-alpha-15/plexus-component-api-1.0-alpha-15.pom
948b downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-classworld
s/1.2-alpha-6/plexus-classworlds-1.2-alpha-6.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0-alp
ha-1/plexus-io-1.0-alpha-1.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-component-
api/1.0-alpha-16/plexus-component-api-1.0-alpha-16.pom
2K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-containers
/1.0-alpha-16/plexus-containers-1.0-alpha-16.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.3/m
aven-archiver-2.3.jar
17K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1
.0-alpha-9/plexus-archiver-1.0-alpha-9.jar
153K downloaded
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0-alp
ha-1/plexus-io-1.0-alpha-1.jar
11K downloaded
[INFO] [jar:jar]
[INFO] Building jar: C:\Users\bhangun\my_app\target\my_app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 1 second
[INFO] Finished at: Tue Oct 14 08:46:30 ICT 2008
[INFO] Final Memory: 12M/254M
[INFO] ------------------------------------------------------------------------

C:\Users\bhangun\my_app>

Nah sekarang jalanin deh, ketikan:

java _cp target/my_app_1.0_SNAPSHOT.jar com.mycompany.app.App

Hasilnya:

Hello World!

Asyik....... gampang kan. Kira-kira projek yang lainnya kayak gitu lah.....
Tapi sebenarnya ada beberapa fase untuk mempaket projek kita. Tapi silahkan coba-coba sendiri yach..... biar lebih puas. Ni fase-fase yang bisa dicoba (perintah package sebenarnya ada ditengah, tapi gk pa-pa coba lagi mulai dari awal fase dibawah ini:

validate : validate the project is correct and all necessary information is available
compile : compile the source code of the project
test : test the compiled source code using a suitable unit testing framework. These tests should
not require the code be packaged or deployed
package : take the compiled code and package it in its distributable format, such as a JAR.
integration test : process and deploy the package if necessary into an environment where
integration tests can be run
verify : run any checks to verify the package is valid and meets quality criteria
install : install the package into the local repository, for use as a dependency in other projects
locally
deploy : done in an integration or release environment, copies the final package to the remote
repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are
clean : cleans up artifacts created by prior builds
site : generates site documentation for this project

Catatan: perlu diingat, kan sebelumnya dikatakan bahwa Maven bakal nyimpen hasil download nya di repository lokal. Iya yach dimana tuh.....!! pingin tau pingin tau.......
Maven bakal nyimpenya di folder .m2. klo contoh di windows kira2 kayk gini:

C:\Users\bhangun\
| -- .m2
| ‘— repository
| ‘—com
| ‘—librari_1
| ‘—librari_2
| ‘—librari_.....


Gampang kan. Ok Selamat mencoba ........!!!!
Alhamdulillah.....

No comments: