附有构建项目实例代码。
下面简要说明下项目架构
1.创建parent项目itp02-parent
其中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> <prerequisites> <maven>3.0</maven> </prerequisites> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <tycho-version>0.20.0</tycho-version> </properties> <modules> <module>tycho.demo.itp02.bundle</module> <module>tycho.demo.itp02.bundle.tests</module> <module>tyco.demo.itp02.feature</module> <module> tyco.demo.itp02.p2Update</module> </modules> <dependencies> <dependency> <groupId>tycho.demo.itp02</groupId> <artifactId>pomfirst-bundle</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>tycho.demo.itp02</groupId> <artifactId>pomfirst-thirdparty</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies> <repositories> <repository> <id>helios</id> <layout>p2</layout> <url>http://download.eclipse.org/releases/helios</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.eclipse.tycho</groupId> <!-- The Tycho Maven Plugin provides extensions to build Eclipse projects. --> <artifactId>tycho-maven-plugin</artifactId> <version>${tycho-version}</version> <extensions>true</extensions> </plugin> <plugin> <groupId>org.eclipse.tycho</groupId> <!-- Tycho Target Platform Configuration Plugin --> <artifactId>target-platform-configuration</artifactId> <version>${tycho-version}</version> <configuration> <pomDependencies>consider</pomDependencies> </configuration> </plugin> </plugins> </build> </project>
2.我们创建普通的Eclipse 插件,名称tycho.demo.itp02.bundle
其pom.xml是这样定义的:
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>tycho.demo.itp02.bundle</artifactId> <packaging>eclipse-plugin</packaging> </project>
在这个项目中,我们使用了如下代码:
/******************************************************************************* * Copyright (c) 2008, 2011 Sonatype Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Sonatype Inc. - initial API and implementation *******************************************************************************/ package tycho.demo.itp02.bundle; import tycho.demo.itp02.pomfirst.PomFirst; public class ITP02 { public String getMessage() { return new PomFirst().getMessage(); } }
这里的PomFirst实际上来自其它的工程(pomfirst-bundle)
3.下面来看下它的pom文件是如何定义的:
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-pomfirst-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>pomfirst-bundle</artifactId> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <executions> <execution> <id>bundle-manifest</id> <phase>process-classes</phase> <goals> <goal>manifest</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
去overview选项卡,你会发现它的packaging:jar
这里它也属与第三方jar包,不过不是从外部引用的2进制jar,而是我们自己定义的jar。
OK,接下来咱看看真正的第三方jar如何定义:
4.我们创建名称为pomfirst-thirdparty工程,其中pom文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-pomfirst-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>pomfirst-thirdparty</artifactId> <packaging>bundle</packaging> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Embed-Dependency> plexus-utils </Embed-Dependency> <_exportcontents> org.codehaus.plexus.util.*;version="2.0.7" </_exportcontents> <Bundle-ClassPath>{maven-dependencies}</Bundle-ClassPath> <Embed-Transitive>true</Embed-Transitive> <Embed-Directory>jars</Embed-Directory> <_failok>true</_failok> <_nouses>true</_nouses> </instructions> </configuration> </plugin> </plugins> </build> </project>
我们将其打成了bundle。
5.最后我们把pomfirst-bundle,pomfirst-thirdparty交给itp02-pomfirst-parent来管理,这个parent的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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> <prerequisites> <maven>3.0</maven> </prerequisites> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-pomfirst-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <tycho-version>0.20.0</tycho-version> <manifest-location>META-INF</manifest-location> </properties> <modules> <module>pomfirst-bundle</module> <module>pomfirst-thirdparty</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>2.0.7</version> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifestFile>${manifest-location}/MANIFEST.MF</manifestFile> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.1.0</version> <configuration> <manifestLocation>${manifest-location}</manifestLocation> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
为了保证我们之前的eclipse-plugin工程(tycho.demo.itp02.bundle)的成功创建,我们必须先构建我们引用的第三方包(这里说的就是被打成jar和bundle的工程)
6.构建测试插件tycho.demo.itp02.bundle.tests
pom.xml内容如下;
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>tycho.demo.itp02.bundle.tests</artifactId> <packaging>eclipse-test-plugin</packaging> </project>
7.为了实现在线更新,我们创建tyco.demo.itp02.feature,其中pom的内容如下:
<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> <artifactId>tyco.demo.itp02.feature</artifactId> <packaging>eclipse-feature</packaging> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> </project>
8.创建站点工程:
<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> <artifactId>tyco.demo.itp02.p2Update</artifactId> <packaging>eclipse-repository</packaging> <parent> <groupId>tycho.demo.itp02</groupId> <artifactId>itp02-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <build> <plugins> <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-p2-director-plugin</artifactId> <version>${tycho-version}</version> <configuration> <formats> <win32>zip</win32> <linux>tar.gz</linux> <macosx>tar.gz</macosx> </formats> </configuration> <executions> <execution> <id>materialize-products</id> <goals> <goal>materialize-products</goal> </goals> </execution> <execution> <id>archive-products</id> <goals> <goal>archive-products</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>