Cодержание
Создайте проект Java OSGi с Maven и Tycho
View more Tutorials:
Статья основана на:
- Eclipse 3.4 (LUNA)
- Tycho 0.20.0
Для начала, нужно удостовериться, что установили Tycho в Eclipse. Если нет, то вы можете посмотреть инструкцию здесь:
Сначала, мы быстро создадим проект OSGi следуя обычному способу


Введите:
- Version: 2.0.0.qualifier
- Activator: org.o7planning.tutorial.simpleosgi.Activator


Add:
- org.eclipse.osgi
- org.eclipse.equinox.console
- org.apache.felix.gogo.command
- org.apache.felix.gogo.runtime
- org.apache.felix.gogo.shell

Activator.java
package org.o7planning.tutorial.simpleosgi; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; System.out.println("SimpleOSGi Started"); } public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; System.out.println("SimpleOSGi Stopped"); } }
Мы быстро конфигурируем и запустим этот OSGi, чтобы увидеть, что состояние только что созданного OSGi является хорошим.
Щелкните правой кнопкой мыши на проект SimpleOSGi, выберите "Run As / Run Configuration .."




Запуск OSGi для получения успешных результатов.

Далее мы быстро создадим Maven Project. Цель это в будущем мы построим 2 проекта Maven Project и Osgi Project используя Maven и Tycho. И видим, что оба проекта обрабатываются одинаково.





CheckNumeric.java
package org.o7planning.tutorial.simplemaven; import org.apache.commons.lang3.StringUtils; public class CheckNumeric { public static void main(String[] args) { String text1 = "0123a4"; String text2 = "01234"; boolean result1 = StringUtils.isNumeric(text1); boolean result2 = StringUtils.isNumeric(text2); System.out.println(text1 + " is a numeric? " + result1); System.out.println(text1 + " is a numeric? " + result2); } }
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> <groupId>org.o7planning</groupId> <artifactId>SimpleMaven</artifactId> <version>3.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> </dependencies> </project>
Цель данного проекта конфигурировать Maven и Tycho чтобы построить 2 проекта (SimpleOSGi & SimpleMaven)
Создайте обычный Project Java


Для начала мы конвертируем SimpleOSGi project в Maven project. Теперь SimpleOSGi является и OSGi project и Maven project.


Project конвертирован в Maven project, и получено оповещение ошибки, вам не нужно волноваться по этому поводу.

Обратите внимание, что если ваш OSGi имеет версю ААА и окончание qualifier, то на Maven вам также необходима конфигурация на pom.xml файле, проект имеет версию AAA и окончание SNAPSHOT. Как на рисунке ниже.![]()
Открыть файл pom.xml и переконфигурировать:
SimpleOSGi/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> <groupId>org.o7planning</groupId> <artifactId>SimpleOSGi</artifactId> <version>2.0.0-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> <parent> <groupId>org.o7planning</groupId> <artifactId>MavenParent</artifactId> <version>1.0.0</version> <relativePath>../MavenParent/pom.xml</relativePath> </parent> </project>
Откройте файл pom.xml на в проекте SimpleMaven и добавьте следующий код:
**
<parent> <groupId>org.o7planning</groupId> <artifactId>MavenParent</artifactId> <version>1.0.0</version> <relativePath>../MavenParent/pom.xml</relativePath> </parent>
SimpleMaven/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> <groupId>org.o7planning</groupId> <artifactId>SimpleMaven</artifactId> <version>3.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> </dependencies> <parent> <groupId>org.o7planning</groupId> <artifactId>MavenParent</artifactId> <version>1.0.0</version> <relativePath>../MavenParent/pom.xml</relativePath> </parent> </project>


MavenParent/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> <groupId>org.o7planning</groupId> <artifactId>MavenParent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 0.22.0 : Target location type: Directory is not supported --> <tycho-version>0.22.0</tycho-version> </properties> <repositories> <!-- Select the P2 repository to be used when resolving dependencies --> <repository> <id>eclipse-luna</id> <layout>p2</layout> <url>http://download.eclipse.org/releases/luna</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>tycho-snapshots</id> <url>https://oss.sonatype.org/content/groups/public/</url> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <!-- enable tycho build extension --> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-maven-plugin</artifactId> <version>${tycho-version}</version> <extensions>true</extensions> <configuration> <dependency-resolution> <optionalDependencies>ignore</optionalDependencies> </dependency-resolution> </configuration> </plugin> <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-versions-plugin</artifactId> <version>${tycho-version}</version> <configuration> <dependency-resolution> <optionalDependencies>ignore</optionalDependencies> </dependency-resolution> </configuration> </plugin> <!-- <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>target-platform-configuration</artifactId> <version>${tycho-version}</version> <configuration> <target> <artifact> <groupId>org.o7planning</groupId> <artifactId>TargetPlatform</artifactId> <version>1.0.0</version> </artifact> </target> </configuration> </plugin> --> </plugins> </build> <modules> <module>../SimpleMaven</module> <module>../SimpleOSGi</module> </modules> </project>
Щелкните правой кнопкой мыши на MavenParent выберите Run As/Maven Install

