External Library

Although theoretically any external library available as a JAR and uploaded to a Maven repository can be added to any project as simple dependency, this approach is not advisable if one wants to use both Maven and eclipse at the same time (many hurtful expierences have shown). The best approach is to make a clear separation between eclipse projects linked to an external dependency and eclipse projects, which introduce new code. Thus, 'empty' projects in eclipse must be created, which contain no 'new' classes but soley a specification of external dependencies.

How to use this template:
  • Create a new pom.xml in an empty Java project (you can create the Java project using eclipse, just make sure to specify src/main/java as source directory and target/classes as output directory).
  • Copy the template below into the pom.xml file and fill out the placeholders ([XXX]) with sensible values.
  • Add the maven dependencies for the external libraries you need (which are not part of your eclipse workspace).
  • Test whether you can create an eclipse project without problems by executing mvn eclipse:clean clean package eclipse:eclipse -Declipse.pde -Declipse.useProjectReferences=false install
Features of this template:
  • Allows to link use external libraries and make them available wrapped in a local eclipse project. This project can be introduced as dependency for other projects in the workspace, allowing these to, indirectly, use the external library/
Constraints of this template:
  • Project can only link to external dependency but must otherwise be 'empty' in that it does not define any 'new' classes in its source folders.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Template: https://sites.google.com/site/eclipsemaven/external-library -->
<project>

    <groupId>[XXX]</groupId>
    <artifactId>[XXX]</artifactId>
    <version>[XXX]</version>
    <name>${project.artifactId}</name>

    <properties>

        <module.importedPackages>
            *

        </module.importedPackages>
        <module.exportedContents>
        </module.exportedContents>
        <module.exportedPackages>
            *
        </module.exportedPackages>
        <module.embeddedDependencies>*
        </module.embeddedDependencies>
        <module.embedTransitive>true</module.embedTransitive>
        <module.serviceDefinitions>
        </module.serviceDefinitions>
    </properties>

    <!-- DEPENDENCIES -->
    <dependencies>
        <dependency>
            <groupId>[XXX]</groupId>
            <artifactId>[XXX]</artifactId>
            <version>[XXX]</version>
        </dependency>

        <dependency>
            <groupId>[XXX]</groupId>
            <artifactId>[XXX]</artifactId>
            <version>[XXX]</version>
        </dependency>

      
    </dependencies>

    <modelVersion>4.0.0</modelVersion>

    <packaging>bundle</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <version>2.3.2</version>
            </plugin>
            <!-- FOR BUNDLE MANAGEMENT -->
            <!-- The Maven bundle plugin generates Meta-data required for OSGi -->

            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.1.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Import-Package>${module.importedPackages}</Import-Package>
                        <Export-Package>${module.exportedPackages}</Export-Package>
                        <Service-Component>${module.serviceDefinitions}
                        </Service-Component>
                        <Embed-Dependency>${module.embeddedDependencies}
                        </Embed-Dependency>
                        <Embed-Directory>target/dependency</Embed-Directory>
                        <Embed-Transitive>${module.embedTransitive}</Embed-Transitive>
                        <_exportcontents>${module.exportedContents}</_exportcontents>
                    </instructions>
                </configuration>
            </plugin>



            <!-- FOR MAVEN ECLIPSE PLUGIN -->
            <!-- Dependency Plugin used to copy the dependency JARs into the root
                project folder. There the Maven eclipse plugin will add them to the classpath
                of PDE projects. -->
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                    <!-- for eclipse pde -->
                    <execution>
                        <id>copy-dependencies2</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- <includeArtifactIds>${module.embeddedDependencies}</includeArtifactIds> -->
                            <outputDirectory>${basedir}/target/dependency</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Cleanup necessary because of PDE tweaks, clear the project directory -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${basedir}</directory>
                            <includes>
                                <include>*.jar</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <!-- Keep the MANIFEST.MF used by eclipse in sync with the MANIFEST.MF
                created by the maven bundle plugin -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <delete file="${basedir}/META-INF/MANIFEST.MF" />
                                <copy file="target/classes/META-INF/MANIFEST.MF" tofile="${basedir}/META-INF/MANIFEST.MF" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>


        <!-- RESOURCES -->
        <resources>
            <!-- This entry makes sure that resources, which lie in the same package
                as Java classes, are copied into the target. Often external libraries require
                resources, which are loaded using Class.getResource or Class.getResourceAsStream
                and which are in a subpackage of the class. For instance, the NetBeans template
                for the Swing Application Framework does so. -->
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <!-- This entry makes sure component definitions for OSGi declarative
                services are copied into the destination -->
            <resource>
                <targetPath>OSGI-INF</targetPath>
                <filtering>false</filtering>
                <directory>OSGI-INF</directory>
                <includes>
                    <include>**</include>
                </includes>
            </resource>
            <!-- I really do not know why know a manual entry for src/main/resources
                is necessary? It should be included following the Maven convention. -->
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**</include>
                </includes>
            </resource>

        </resources>


    </build>

    <!-- ADDITIONAL REPOSITORIES -->
    <!-- define non Maven central repositories here -->
    <!-- <repositories>
        <repository>
            <id>Jenabean</id>
            <url>http://jenabean.googlecode.com/svn/repo</url>
        </repository>
    </repositories>
    <-->

</project>






Comments