1

I have non-maven java project. I want to create second project, with maven nature this time to hold all the integration and unit tests for the main application. To run tests from CLI I need to include dependencies from main project into test project in "maven style". As there is no out-of-the-box solution for that I have decided to crate new Maven plugin for that:

So, my plugin would analisye .classpath file of Eclipse and add proper URLs to the collection of compiletime libraries. How can I inject such dependencies from maven plugin?

I have found something on the internet like this:

    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    private MavenProject project;

and project has good looking methods like getCompileClasspathElements(). However for some reasons I cannot use it, because of unresolved dependencies:

Description Resource Path Location Type The project was not built since its build path is incomplete. Cannot find the class file for org.apache.maven.artifact.DependencyResolutionRequiredException. Fix the build path then try building this project include-eclipse-project-maven-plugin Unknown Java Problem

Where can I find this class? Shouldnt it be included in maven-project-plugin as dependency somewhere? What is correct approach for classpath modification on maven lifecycle runtime?

Here is my POM for the plugin:

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>3.0.8</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.4</version>
            <configuration>
                <goalPrefix>include-eclipse-project</goalPrefix>
                <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
            </configuration>
            <executions>
                <execution>
                    <id>mojo-descriptor</id>
                    <goals>
                        <goal>descriptor</goal>
                    </goals>
                </execution>
                <execution>
                    <id>help-goal</id>
                    <goals>
                        <goal>helpmojo</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>
</build>
<profiles>
    <profile>
        <id>run-its</id>
        <build>

            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-invoker-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <debug>true</debug>
                        <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
                        <pomIncludes>
                            <pomInclude>*/pom.xml</pomInclude>
                        </pomIncludes>
                        <postBuildHookScript>verify</postBuildHookScript>
                        <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
                        <settingsFile>src/it/settings.xml</settingsFile>
                        <goals>
                            <goal>clean</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>install</goal>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>

        </build>
    </profile>
</profiles>

EDIT: After changing dependencies in POM, plugin is compilable however injected artifcats into MavenProject object are ommited during compilation phase.

I am adding deps via

public void setDependencyArtifacts(Set<Artifact> dependencyArtifacts)

And listing via

public Set<Artifact> getArtifacts()
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Yes, this is my point, I am using surefire for running tests. But what I need to test, is in different project and I cannot include it as maven dependency thus I have compiletime errors about missing classes while running tests. – Antoniossss Dec 14 '15 at 09:45
  • @A.DiMatteo ofc, but this is non-maven project. Openfire http://www.igniterealtime.org/projects/openfire/ As I mentioned - cannot include as maven deps as they are NOT MAVEN PROJECTS. Output is a set of jars and extensions. Dunno how to wrap it into maven artifact for installation – Antoniossss Dec 14 '15 at 09:47
  • And as there are many dependencies in the Openfire, I would like to automate the process of including those into compile/runtime classpath insteed of adding manually extra entry into the POM for every jar lib in main project, as well as adding additional sources directories for the classas I actually want to test - those are part of Openfire's project structure. – Antoniossss Dec 14 '15 at 09:52
  • would mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt help you? It would print the full classpath (dependencies and transitive dependencies) to the supplied file. – A_Di-Matteo Dec 14 '15 at 10:15
  • I want to do exact opposite thing. I want to read external file with paths to some jar's and add them to maven's compile classpath – Antoniossss Dec 14 '15 at 10:20

0 Answers0