You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by fhomasp <th...@realdolmen.com> on 2010/12/03 15:35:35 UTC

possible ${} expressions on MOJO parameters

Hey,

I was working on a Mojo on which I thought would be a trivial problem. 
Though it turns out it's not.

I simply want to add a property file during the phase "generate-resources"
with the current version, on the classpath.  Ideally it's located at the
default resourcesdir.  The name of the props file needs to be the name of
the artifact (or the given name).

For one, I was looking for a maven property like
${build.rescourceDirectory}.  But I couldn't find it.  It's of course kind
of logical as It's not guaranteed to be only one directory.  But that's not
the main reason of this thread.

The other triviality seems to be setting a parameter to use a default-value
for this but it doesn't work, or at least it returns null.

the properties, which of course exist in maven and don't work in the mojo,
are: 

    /**
     * @parameter expression="${versionprop.appName}"
default-value="${build.artifactId}"
     */
    private String appName;


    /**
     * @parameter expression="${versionprop.name}" default-value="${name}"
     */
    private String name;

 /**
     * @parameter expression="${versionprop.test}"
default-value="${artifactId}"
     */
    private String test;

I do find this very odd to say the least.  I'd expect to be able to use
every expression value used in maven in a Mojo.

I know it's really simple to just put these values in the configuration
section, however it would really be better if it would work without config
to minimize overhead and human errors.


Any idea's?

Thanks!

-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3290963.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
I suspected as much... but if you just dump the resource in target/classes
the IDE's may not see the properties file as being _source_ and autocomplete
will be flakey

On 6 December 2010 10:33, Antonio Petrelli <an...@gmail.com>wrote:

> 2010/12/6 Stephen Connolly <st...@gmail.com>:
> > And the reason for adding it as a Resource in a separate folder is so
> that
> > IDE's can recognise that the file is a source file but generated source.
> >
> > For example IntelliJ IDEA will give auto-completion of property names
> from
> > .properties files if the property file is listed as a generated source
> root
> > by using the Resource mechanism
>
> M2Eclipse does it too.
>
> Antonio
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: possible ${} expressions on MOJO parameters

Posted by fhomasp <th...@realdolmen.com>.
I will make sure that the generated-resource difference is explained, it does
make sense.  But for now it's being placed in the resources root by default. 
I did add a config adjustment that allows for custom paths for the file.  If
it's not a sibling of a resources folder it gets added as a Resource.

You all have been very helpful.  Context is key to understanding why and how
:-)
-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293810.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Antonio Petrelli <an...@gmail.com>.
2010/12/6 Stephen Connolly <st...@gmail.com>:
> And the reason for adding it as a Resource in a separate folder is so that
> IDE's can recognise that the file is a source file but generated source.
>
> For example IntelliJ IDEA will give auto-completion of property names from
> .properties files if the property file is listed as a generated source root
> by using the Resource mechanism

M2Eclipse does it too.

Antonio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
And the reason for adding it as a Resource in a separate folder is so that
IDE's can recognise that the file is a source file but generated source.

For example IntelliJ IDEA will give auto-completion of property names from
.properties files if the property file is listed as a generated source root
by using the Resource mechanism

On 6 December 2010 09:14, fhomasp <th...@realdolmen.com> wrote:

>
>
> Ah I see, thanks
>
>
> Antonio Petrelli wrote:
> >
> > 2010/12/6 fhomasp <th...@realdolmen.com>:
> >>
> >>
> >> If the File has already been written to the target folder (whatever that
> >> may
> >> be ^^ ) why would I need to add it as a Resource Object to the project?
> >
> > It's not in the target/classes folder, but in a folder of target,
> > sibling of classes.
> > You have to add it to the resources to be packaged together.
> >
> > Antonio
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293739.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: possible ${} expressions on MOJO parameters

Posted by fhomasp <th...@realdolmen.com>.

Ah I see, thanks


Antonio Petrelli wrote:
> 
> 2010/12/6 fhomasp <th...@realdolmen.com>:
>>
>>
>> If the File has already been written to the target folder (whatever that
>> may
>> be ^^ ) why would I need to add it as a Resource Object to the project?
> 
> It's not in the target/classes folder, but in a folder of target,
> sibling of classes.
> You have to add it to the resources to be packaged together.
> 
> Antonio
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
> 
> 

-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293739.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Antonio Petrelli <an...@gmail.com>.
2010/12/6 fhomasp <th...@realdolmen.com>:
>
>
> If the File has already been written to the target folder (whatever that may
> be ^^ ) why would I need to add it as a Resource Object to the project?

It's not in the target/classes folder, but in a folder of target,
sibling of classes.
You have to add it to the resources to be packaged together.

Antonio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by fhomasp <th...@realdolmen.com>.

If the File has already been written to the target folder (whatever that may
be ^^ ) why would I need to add it as a Resource Object to the project?  



stephenconnolly wrote:
> 
> The Resource is used to reference the folder containing the resources to
> add.
> 
> You create a subfolder of target (convention is to call it
> target/generated-resources/yourpluginname)
> You write the properties file in that folder... e.g. if you want to have
> th
> properties file end up in META-INF/version.properties you would write
> target/generated-resources/yourpluginname/META-INF/version.properties
> 
> You create a new Resource object with the root being
> target/generated-resources/yourpluginname and if you don't need filtering
> set filtering to false
> 
> Add the resource object to the Project
> 
> Done
> 
> On 6 December 2010 08:19, fhomasp <th...@realdolmen.com> wrote:
> 
>>
>> Hey,
>>
>> Thanks for the suggestions, I guess I'll be able to get it working using
>> a
>> combination of the suggestions :-)
>>
>> However at this point I'm unsure how I'd go about adding content to a new
>> Resource file.  So at this point I'm still using a FileOutputStream to
>> store
>> a Properties file.
>>
>> Any extra suggestions? :-)
>>
>> Thanks already
>> --
>> View this message in context:
>> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293698.html
>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293735.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
The Resource is used to reference the folder containing the resources to
add.

You create a subfolder of target (convention is to call it
target/generated-resources/yourpluginname)
You write the properties file in that folder... e.g. if you want to have th
properties file end up in META-INF/version.properties you would write
target/generated-resources/yourpluginname/META-INF/version.properties

You create a new Resource object with the root being
target/generated-resources/yourpluginname and if you don't need filtering
set filtering to false

Add the resource object to the Project

Done

On 6 December 2010 08:19, fhomasp <th...@realdolmen.com> wrote:

>
> Hey,
>
> Thanks for the suggestions, I guess I'll be able to get it working using a
> combination of the suggestions :-)
>
> However at this point I'm unsure how I'd go about adding content to a new
> Resource file.  So at this point I'm still using a FileOutputStream to
> store
> a Properties file.
>
> Any extra suggestions? :-)
>
> Thanks already
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293698.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: possible ${} expressions on MOJO parameters

Posted by fhomasp <th...@realdolmen.com>.
Hey,

Thanks for the suggestions, I guess I'll be able to get it working using a
combination of the suggestions :-)

However at this point I'm unsure how I'd go about adding content to a new
Resource file.  So at this point I'm still using a FileOutputStream to store
a Properties file.

Any extra suggestions? :-)

Thanks already
-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3293698.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Antonio Petrelli <an...@gmail.com>.
2010/12/3 fhomasp <th...@realdolmen.com>:
> I simply want to add a property file during the phase "generate-resources"
> with the current version, on the classpath.  Ideally it's located at the
> default resourcesdir.  The name of the props file needs to be the name of
> the artifact (or the given name).

You need to have the Maven Project:
<snip>
/**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;
</snip>

Then interate the resources:
<snip>
project.getResources()
</snip>

However I support the suggestion of Stephen, create a new directory
under "target" and add it as a resource.

Oh, and I "command you to follow the Maven way" :-D

Antonio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
I am explaining how the plugin has to work to do what you want. see my later
example where I give you most of the code you will need

On 3 December 2010 15:33, fhomasp <th...@realdolmen.com> wrote:

>
> That's not an answer...
>
> Oh and I will? I'm not a native English speaker, but it does sound like
> you're ordering me ;-)
>
> I know it is not the maven way, but this is the requirement.  It's not in
> my
> power to adjust it, or at least not just yet.
>
> But thanks anyway
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3291037.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: possible ${} expressions on MOJO parameters

Posted by fhomasp <th...@realdolmen.com>.
That's not an answer...

Oh and I will? I'm not a native English speaker, but it does sound like
you're ordering me ;-)

I know it is not the maven way, but this is the requirement.  It's not in my
power to adjust it, or at least not just yet.

But thanks anyway
-- 
View this message in context: http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3291037.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
/**
 * Generates the source files.
 *
 * @author connollys
 * @goal generate-sources
 * @phase generate-sources
 * @since Oct 20, 2009 9:04:44 AM
 */
public class GenerateSourcesMojo
    extends AbstractMojo
{
    /**
     * The current Maven project
     *
     * @parameter expression="${project}"
     * @readonly
     * @required
     */
    private MavenProject project;

    /**
     * The encoding to use when reading source files.
     *
     * @parameter default-value="${project.build.sourceEncoding}"
     */
    private String sourceEncoding;

    /**
     * The encoding to use when generating Java source files.
     *
     * @parameter default-value="${project.build.outputEncoding}"
     */
    private String outputEncoding;

    /**
     * The directory where the generated resource files will be
stored. The directory will be registered as a resource
     * root of the project such that the generated files will
participate in later build phases like packaing.
     *
     * @parameter
expression="${project.build.directory}/generated-resources/gen-bsapi"
     * @required
     */
    private File resourceOutputDirectory;

    /**
     * {@inheritDoc}
     */
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        if ( StringUtils.isEmpty( sourceEncoding ) )
        {
            getLog().warn( "Source file encoding has not been set,
using platform encoding "
                + System.getProperty( "file.encoding" ) + ", i.e.
build is platform dependent!" );
            sourceEncoding = System.getProperty( "file.encoding" );
        }
        if ( StringUtils.isEmpty( outputEncoding ) )
        {
            getLog().warn( "Output file encoding has not been set,
using platform encoding "
                + System.getProperty( "file.encoding" ) + ", i.e.
build is platform dependent!" );
            outputEncoding = System.getProperty( "file.encoding" );
        }

        Resource resource = new Resource();
        ...
        this.project.addResource( resource );
    }

}



On 3 December 2010 15:27, Stephen Connolly
<st...@gmail.com>wrote:

> You will create a folder at
> ${basedir}/target/generated-resources/mypluginname/
> You will put your generated file in that directory
> You will add that directory to the project's list fo resource directories
> You will find the above solution is the maven way.
>
> Putting generated things in folders which are not sub-folders of target is
> not the maven way.
>
> -Stephen
>
>
> On 3 December 2010 14:35, fhomasp <th...@realdolmen.com> wrote:
>
>>
>> Hey,
>>
>> I was working on a Mojo on which I thought would be a trivial problem.
>> Though it turns out it's not.
>>
>> I simply want to add a property file during the phase "generate-resources"
>> with the current version, on the classpath.  Ideally it's located at the
>> default resourcesdir.  The name of the props file needs to be the name of
>> the artifact (or the given name).
>>
>> For one, I was looking for a maven property like
>> ${build.rescourceDirectory}.  But I couldn't find it.  It's of course kind
>> of logical as It's not guaranteed to be only one directory.  But that's
>> not
>> the main reason of this thread.
>>
>> The other triviality seems to be setting a parameter to use a
>> default-value
>> for this but it doesn't work, or at least it returns null.
>>
>> the properties, which of course exist in maven and don't work in the mojo,
>> are:
>>
>>    /**
>>     * @parameter expression="${versionprop.appName}"
>> default-value="${build.artifactId}"
>>     */
>>    private String appName;
>>
>>
>>    /**
>>     * @parameter expression="${versionprop.name}" default-value="${name}"
>>     */
>>    private String name;
>>
>>  /**
>>     * @parameter expression="${versionprop.test}"
>> default-value="${artifactId}"
>>     */
>>    private String test;
>>
>> I do find this very odd to say the least.  I'd expect to be able to use
>> every expression value used in maven in a Mojo.
>>
>> I know it's really simple to just put these values in the configuration
>> section, however it would really be better if it would work without config
>> to minimize overhead and human errors.
>>
>>
>> Any idea's?
>>
>> Thanks!
>>
>> --
>> View this message in context:
>> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3290963.html
>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>>
>

Re: possible ${} expressions on MOJO parameters

Posted by Stephen Connolly <st...@gmail.com>.
You will create a folder at
${basedir}/target/generated-resources/mypluginname/
You will put your generated file in that directory
You will add that directory to the project's list fo resource directories
You will find the above solution is the maven way.

Putting generated things in folders which are not sub-folders of target is
not the maven way.

-Stephen

On 3 December 2010 14:35, fhomasp <th...@realdolmen.com> wrote:

>
> Hey,
>
> I was working on a Mojo on which I thought would be a trivial problem.
> Though it turns out it's not.
>
> I simply want to add a property file during the phase "generate-resources"
> with the current version, on the classpath.  Ideally it's located at the
> default resourcesdir.  The name of the props file needs to be the name of
> the artifact (or the given name).
>
> For one, I was looking for a maven property like
> ${build.rescourceDirectory}.  But I couldn't find it.  It's of course kind
> of logical as It's not guaranteed to be only one directory.  But that's not
> the main reason of this thread.
>
> The other triviality seems to be setting a parameter to use a default-value
> for this but it doesn't work, or at least it returns null.
>
> the properties, which of course exist in maven and don't work in the mojo,
> are:
>
>    /**
>     * @parameter expression="${versionprop.appName}"
> default-value="${build.artifactId}"
>     */
>    private String appName;
>
>
>    /**
>     * @parameter expression="${versionprop.name}" default-value="${name}"
>     */
>    private String name;
>
>  /**
>     * @parameter expression="${versionprop.test}"
> default-value="${artifactId}"
>     */
>    private String test;
>
> I do find this very odd to say the least.  I'd expect to be able to use
> every expression value used in maven in a Mojo.
>
> I know it's really simple to just put these values in the configuration
> section, however it would really be better if it would work without config
> to minimize overhead and human errors.
>
>
> Any idea's?
>
> Thanks!
>
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/possible-expressions-on-MOJO-parameters-tp3290963p3290963.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>