Créer un projet Java OSGi avec Maven et Tycho
View more Tutorials:


Ce document est basé sur :
- Eclipse 3.4 (LUNA)
- Tycho 0.20.0
Tout d'abord, vous devez vous assurer que vous avez installé Tycho sur Eclipse. Si vous ne l'avez pas installé, vous pouvez voir les instructions ici
Tout d'abord, nous créons rapidement un projet OSGi de manière habituelle.


Saississez :
- 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");
}
}
Nous allons configurer rapidement et exécuter ce projet OSGi, afin de voir que le statut de l'OSGi créé est bon.
Cliquez sur le bouton droit sur project SimpleOSGi, sélectionnez "Run As/Run Configuration.."




L'exécution OSGi pour obtenir des résultats réussis.

Ensuite, nous créerons rapidement Maven Project. Nos buts est que nous allons construire deux projets projet Maven & projet Osgi utilisant Maven & Tycho. Et nous allons trouver que les deux projets sont traités de la même manière.





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>
L'objectif de la création ce projet est de configurer Maven&Tycho pour construire deux projets sur (SimpleOSGi&SimpleMaven)
Créez un projet Java normal


D'abord, nous avons convertir le projet SimpleOSGi en projet Maven. Maintenant, SimpleOSGi est à la fois un projet OSGi et un projet Maven.


Le projet a été converti en projet Maven et a un message d'erreur, n'en sois pas inquiet.

Notez que si la version de configuration de votre projet OSGi est celle de AAA et et le suffixe est qualifier, alors sur Maven, vous devez également configurer dans le fichier pom.xml, la version AAA et le suffixe SNAPSHOT. Comme l'illustration ci-dessous.![]()
Ouvrez le fichier pom.xml et reconfigurez - le comme suivant :
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>
Ouvrez le fichier pom.xml sur le projet SimpleMaven et ajoutez le code suivant :
**
<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>
Cliquez sur le bouton droit du MavenParent et sélectionnez Run As / Maven Install.

