views:

174

answers:

2

Hi All,

I have in-house developed maven plugin(lets call it A) which uses spring IOC container and a maven project(call it B) that uses this plugin.

I want to specify particular plugin configuration in B's pom.xml, so this property will be accessed in plugin's spring context

I expect something like this, pom.xml (project B)

<plugin>
...
 <configuration>
    <dummyproperty>dummy_value</dummyproperty>
 </configuration>
...
</plugin>

Spring_context.xml (project A - plugin)

<bean class="com.blabla.SomeClass">
 <property name="someBeanProperty" value="${dummyproperty}" />
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 

I've tried various approaches with using plugin configuration, , additional property files, but placeholders in spring context not get populated with real values. Asking for your help with this

Thanks in advance

A: 

I've implemented exactly the same kind of plugin. The solution was to have a Properties member in the plugin mojo, which is then configured using the usual <cofiguration> section.

The plugin mojo was responsible for loading the spring XML and creating the app context. Before using the app context, the mojo instantiated a PropertiesPlaceHolderConfigurer, setting the properties to the Properties instance configured in the mojo.

For convenience, I also had a Properties instance where the values were ignored, and instead fetched from the project. This allows properties to be conveniently set in the spring config with the same values as they were in the maven project.

mdma
I've tried your solution and it works fine for me.Thanks
diy
A: 

I've tried to follow plexus-based approach recommended by anhin4v and non succeded in making this approach work

Here is pom.xml of dummy app that uses maven plugin:

<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"&gt;
    <modelVersion>4.0.0</modelVersion>
    <groupId>local</groupId>
    <artifactId>dummy-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dummy user</name>
    <build>
        <plugins>
            <plugin>
                <groupId>local</groupId>
                <artifactId>dummy</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <executions>
                    <execution>
                        <id>do somethign</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>touch</goal>
                        </goals>
                        <configuration>
                            <dummyproperty>12345678</dummyproperty>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project> 

Below is code of one of plugins classes that should populate properties from dummy-user project's maven configuration

public class DummyObject {

private String dummyProperty;

/**
 * 
 * @parameter expression="${touch.dummyproperty}"
 */
public void setDummyProperty(String dummyProperty) {
    this.dummyProperty = dummyProperty;
}

public String getDummyProperty() {
    return dummyProperty;
}

This is how DummyObject is used in mojo

/**
 * 
 * @goal touch
 * 
 * @phase process-sources
 */
public class MyMojo extends AbstractMojo {

    public void execute() throws MojoExecutionException {

        DummyObject a = new DummyObject();
        System.out.println(a.getDummyProperty());
    }

However, i still don't see dummyProperty begin populated. Could anybody tell me, what am i doing wrong? Thanks

diy