Maven2 plug-in development details

Source: Internet
Author: User
Thanks to the powerful features of maven2, many companies have gradually switched from ant to maven2, and since maven2 already supports running ant scripts, this greatly reduces the difficulty required by the development team to transition from ant to maven2. Compared with ant's relatively open attitude to developers, maven2 Looks transparent to developers. Many old ant programmers
For example, the problem of writing target has become some simple configuration in maven2. In theory, the starting point of maven2 is much lower than ant, because
Programmers in ant should be at least familiar with the velocity syntax. However, maven2 has no Chinese documents or documents of the complete set of systems for viewing so far
It does not seem much simpler than ant. On the contrary, because everything needs to be configured, but there are no relevant instructions at hand, many developers have no way to start using maven2.
Feeling. What's even more painful is that many Maven documents on the Internet are for Version 1.0. What's more annoying is that many documents are clearly written based on Version 1.0, but our authors only change their titles and
As a result, the keyword information is posted as 2.0 of the information in your blog (extremely irresponsible ).
The previous section is nonsense, just to introduce everyone to my topic. In the process of using Maven for project management, we may want to integrate the existing ant script into our pom.
We even want to integrate the previously written and used engineering scripts for a long time into our existing Maven project without the ant script in pom. At the same time, or the existing
Maven cannot support some special tasks that need to be completed. This topic is naturally in front of us, that is, the plug-in development of maven2. Speaking of maven2, we have to talk about the standard path structure (for example) of maven2 ). <Sourcedirectory> src/main/Java </sourcedirectory> <scriptsourcedirectory> src/main/scripts </scriptsourcedirectory> <testsourcedirectory> src/test/Java </testsurcedirectory> <outputdirectory> Target /classes </outputdirectory> <testoutputdirectory> Target/test-classes </testoutputdirectory> These paths are the basis for maven2 configuration (some other paths are not described in detail). Generally, we do not need to explicitly declare the path structure. Unless we want to change the standard path structure, we can configure the above parameters for personalized configuration.
. However, the official website of maven2 strongly recommends using the standard Maven path. In addition, some plug-ins officially provided by Maven only support standard paths. For non-standard paths, these configurations
(For example, cobertura1.8 plug-in ). (Plug-in of cobertura1.8 officially provided by maven2 has some bugs, and the final coverage test reporting cannot be correctly generated. I plan to reopen a special topic for future reference .) Therefore, the plug-in development I introduced in this article also adopts the standard path of maven2. Let's talk about ant plug-in development, and then introduce how to use Java for plug-in development. Ant Of Maven2 plug-in DevelopmentOne obvious difference between Maven and ant is that it clarifies the version information of the resource package used in the project. Currently, we
A large number of free and open-source third-party plug-ins are required, and these plug-ins are sometimes not ideal for forward compatibility during the upgrade process. I want to change the package structure after Hibernate is upgraded to 3.0.
This makes many developers confused at the beginning, but maven2 can clearly specify the version information of the resource package, thus avoiding confusion in the project due to the resource package version.
Program crash. The plug-in development of the ant script requires two resource packages to be declared in POM. One is the resource package used to compile and package the JAR file of plug-in, one is the resource package required to run the ant script when plug-in is executed. The configuration of POM is as follows: <Project> <modelversion> 4.0.0 </modelversion> <groupid> Org. myproject. plugins </groupid> <artifactid> hello-plugin </artifactid> <version> 1.0-Snapshot </version> <packaging> Maven-plugin </packaging> <Name> Hello plugin </Name> <dependencies> <dependency> <groupid> Org. apache. maven </groupid> <artifactid> Maven-script-ant </artifactid> <version> 2.0.1 </version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactid> Maven-plugin </artifactid> <dependencies> <dependency> <groupid> Org. apache. maven </groupid> <artifactid> Maven-plugin-tools-ant </artifactid> <version> 2.0.1 </version> </dependency> </dependencies> <configuration> <goalprefix> Hello </goalprefix> </configuration> </plugin> </plugins> </build> </Project> It can be seen that I have not made any explicit declaration on the maven2 path here. According to the standard path of maven2, we need-
Put the script file in the in project in the src/main/scripts PATH. Therefore, first create such a directory in the project, and then write the ant script in the directory.
(Hello. Build. XML ): <Project> <target name = "hello"> <echo> ant script echos: Hello, world </echo> </Target> </Project> Next, write the mojo script (hello. Mojos. XML). mojo is the key to ant script execution.
The ant script is placed in the same directory src/main/scripts. In this file, define the goal of maven2 and the target name in the ant script to be executed. Details
The content is as follows: <Pluginmetadata> <Mojos> <mojo> <goal> Hello </goal> <call> Hello </call> <description> say hello, world. </description> </mojo> </Mojos> </pluginmetadata> So far, a complete ant plug-in based on maven2 has been developed. For the sake of convenience, the ant script I wrote here is quite simple. You can make full use of it here based on your specific project needs. Next, execute "MVN install". If the prompt is as follows, it indicates that it has been successfully published to the local repository: D:/Eclipse/workspace/Hello-plugin> MVN install-e-o + Error stacktraces are turned on. [Info] Note: Maven is executing in offline mode. Any artifacts not already in your local Repository will be inaccessible. [Info] scanning for projects... [Info] ---------------------------------------------------------------------------- [Info] building Hello plugin [Info] task-segment: [install] [Info] ---------------------------------------------------------------------------- [Info] [Plugin: descriptor] [Warning] Goal prefix is: Hello; Maven currently expects it to be hello [Info] using 3 extractors. [Info] applying Extractor for language: Java [Info] Extractor for language: Java found 1 mojo descriptors. [Info] applying Extractor for language: bsh [Info] Extractor for language: bsh found 0 mojo descriptors. [Info] applying Extractor for language: ant [Info] Extractor for language: ant found 1 mojo descriptors. [Info] [resources: Resources] [Info] Using default encoding to copy filtered resources. [Info] [compiler: Compile] [Info] Nothing to compile-All classes are up to date [Info] [resources: testresources] [Info] Using default encoding to copy filtered resources. [Info] [Compiler: testcompile] [Info] No sources to compile [Info] [surefire: Test] [Info] No tests to run. [Info] [jar: jar] [Info] building jar: D:/Eclipse/workspace/Hello-plugin/target/hello-plugin-1.0-S Napshot. Jar [Info] [Plugin: addpluginartifactmetadata] [Info] [install: Install] [Info] installing D:/Eclipse/workspace/Hello-plugin/target/hello-plugin-1.0-SNAP Shot. jar to E:/maven-2.0.4/. m2/Repository/org/myproject/plugins/Hello-plugin/1.0 -Snapshot/hello-plugin-1.0-SNAPSHOT.jar [Info] [Plugin: updateregistry] [Info] ------------------------------------------------------------------------ [Info] build successful [Info] ------------------------------------------------------------------------ [Info] total time: 3 seconds [Info] finished at: Thu Jun 28 10:39:02 CST 2007 [Info] final memory: 9 m/17 m [Info] ------------------------------------------------------------------------  D:/Eclipse/workspace/Hello-plugin> Next, you can integrate the plug-in your project. The specific practice is to add the following code to the POM of your project: <Build> <plugins> <plugin> <groupid> Org. myproject. plugins </groupid> <artifactid> hello-plugin </artifactid> <version> 1.0-Snapshot </version> </plugin> </plugins> </build> Of course, maven2 does not only support ant scripts at the plug-in level. You can also directly use ant scripts in POM to complete some tasks. To ensure the integrity of this article, I plan to write another article on this topic. Java Of Maven2 plug-in DevelopmentIntroduce the following code into the ant plug-in project that has already been established (for struts programmers, is it a bit familiar !) : PackageOrg.danlley.com; ImportOrg. Apache. Maven. plugin. abstractmojo; ImportOrg. Apache. Maven. plugin. mojoexecutionexception;/*** says "hi" to the user .* @ GoalSayhi */ Public ClassGreetingmojo ExtendsAbstractmojo { Public VoidExecute () ThrowsMojoexecutionexception {getlog (). info ("-----------------------------------------"); getlog (). info ("| Hello, world. | "); getlog (). info ("-------------------------------------------");}} Both abstractmojo and mojoexecutionexception are classes in the maven2 core support package,
You can find the maven-core-2.0.6-uber.jar In the lib directory of the maven2 installation path (I use version2.0.6, but now I'm
On the official website, 2.0.7 has also been released.) re-compile and re-install. Create a project to test whether the plug-in works properly. The new project Pom is as follows: <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/maven-v4_0_0.xsd"> <modelversion> 4.0.0 </modelversion> <groupid> partition </groupid> <artifactid> mvnproj </artifactid> <packaging> jar </packaging> </ version> 1.0-Snapshot </version> <Name> mvnproj </Name> <URL> http://maven.apache.org </URL> <build> <plugins> <plugin> <groupid> Org. myproject. plugins </groupid> <artifactid> hello-plugin </artifactid> <version> 1.0-Snapshot </version> </plugin> </plugins> </build> <dependencies> <dependency> <groupid> Org. apache. ant </groupid> <artifactid> ant </artifactid> <version> 1.7.0 </version> </dependency> <groupid> JUnit </groupid> <artifactid> JUnit </artifactid> <version> 3.8.1 </version> <scope> test </scope> </dependency> </dependencies> </Project> Run the command. If the running result is as follows, the plug-in is successfully developed! E:/myproj/mvnproj> MVN package Hello: hello: sayhi-e-o + error stacktraces are turned on. [info] Note: Maven is executing in offline mode. any artifacts not already in your localrepository will be inaccessible. [info] scanning for projects... [info] Searching repository for plugin with prefix: 'Hello '. [info] detail [info] building mvnproj [info] task-segment: [Package] [info] ------------------------------------------------------------------------ [info] [resources: resources] [info] Using default encoding to copy filtered resources. [info] [compiler: Compile] [info] Nothing to compile-All classes are up to date [info] [resources: testresources] [info] Using default encoding to copy filtered resources. [info] [Compiler: testcompile] [info] Nothing to compile-All classes are up to date [info] [surefire: Test] [info] surefire report Directory: e: /myproj/mvnproj/target/surefire-Reports --------------------------------------------------------- t e s restart running org.danlley.com. apptesttests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 0.047 secresults: tests run: 1, failures: 0, errors: 0, skipped: 0 [info] [jar: jar] [info] building jar: e: /myproj/mvnproj/target/mvnproj-1.0-SNAPSHOT.jar [info] parts [info] building mvnproj [info] task-segment: [Hello: Hello] (aggregator-style) [info] sequence [info] [Hello: Hello] HELLO: [Echo] ant script echos: Hello, world [info] sequence [info] building mvnproj [info] task-segment: [Hello: sayhi] [info] Quit [info] [Hello: sayhi] [info] --------------------------------------------- [info] | Hello, world. | [info] ----------------------------------------- [info] resume [info] build successful [info] resume [info] total time: 4 seconds [info] finished: thu Jun 28 12:58:51 CST 2007 [info] final memory: 5 m/10 m [info] --------------------------------------------------------------------------  
From: http://blog.chinaunix.net/u/22374/showart.php? Id = 330484

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.