You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by "Nick Cuneo (ncuneo)" <nc...@cisco.com> on 2012/11/16 22:53:20 UTC

Properties within source code

Hi,

We are attempting to migrate our build environment from being very Ant-centric, to be Maven-centric.  We have a source file (Trace.java) that has a couple of variables that get set for debug/non-debug builds.  The code is as follows
public boolean DEBUG = ${debug.enabled};

Obviously this won't compile as is, but with Ant we were able to replace that property at compile time so it would build the appropriate package.  I'm trying to get that same functionality with Maven... I've looked into trying to add the file as a resource and creating the property in the pom.xml.  I've also tried using the google-replace plugin, but that didn't seem to work well either.  Am I just not doing it right?  I'm learning Maven as I go so I figured that's a possibility.  I've tried doing searches on the subject with google and looking through maven documentation and it feels straightforward but just not working for me.

Example from pom:
<properties>
                                <debug.enabled>true</debug.enabled>
</properties>
<build>
...
<resources>
                <resource>
                                <directory>${basedir}/src/trace</directory>
                                <includes>
                                                <include>Trace.java</include>
                                </includes>
                </resource>
</resources>

I see in the log that it's doing something with the file, maybe not what I was intending though:
[INFO] Copying 1 resource

Any help would be appreciative.


Thanks,
     Nick
[Description: http://www.cisco.com/web/europe/images/email/signature/logo02.jpg]

Nicholas Cuneo
Software Engineer
SPVTG
ncuneo@cisco.com<ma...@cisco.com>
Phone: 1-714-434-2344
Mobile: 1-949-243-4952



3500 Hyland Ave
Costa Mesa, CA
United States
Cisco.com<http://www.cisco.com/>

Download vCard[Description: Virtual Card File]<http://wwwin-tools.cisco.com/dir/vcard/ncuneo.vcf>


[Description: Think before you print.]Think before you print.
This email may contain confidential and privileged material for the sole use of the intended recipient. Any review, use, distribution or disclosure by others is strictly prohibited. If you are not the intended recipient (or authorized to receive for the recipient), please contact the sender by reply email and delete all copies of this message.
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html







Re: Properties within source code

Posted by Anders Lindgren <an...@cinnober.com>.
Hi Nick,

I think you missed the filtering part. I are supposed to enable the
filtering by <filertering>true</filtering>
See
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

As you can see there, it also possible to put you variables in a
separate properties file.

Hopes it helps

/Anders

On Fri, 2012-11-16 at 22:53 +0100, Nick Cuneo (ncuneo) wrote:
> Hi,
> 
>  
> 
> We are attempting to migrate our build environment from being very
> Ant-centric, to be Maven-centric.  We have a source file (Trace.java)
> that has a couple of variables that get set for debug/non-debug
> builds.  The code is as follows
> 
> public boolean DEBUG = ${debug.enabled};
> 
>  
> 
> Obviously this won’t compile as is, but with Ant we were able to
> replace that property at compile time so it would build the
> appropriate package.  I’m trying to get that same functionality with
> Maven… I’ve looked into trying to add the file as a resource and
> creating the property in the pom.xml.  I’ve also tried using the
> google-replace plugin, but that didn’t seem to work well either.  Am I
> just not doing it right?  I’m learning Maven as I go so I figured
> that’s a possibility.  I’ve tried doing searches on the subject with
> google and looking through maven documentation and it feels
> straightforward but just not working for me.
> 
>  
> 
> Example from pom:
> 
> <properties>
> 
>                                 <debug.enabled>true</debug.enabled>
> 
> </properties>
> 
> <build>
> 
> …
> 
> <resources>
> 
>                 <resource>
> 
> 
> <directory>${basedir}/src/trace</directory>
> 
>                                 <includes>
> 
> 
> <include>Trace.java</include>
> 
>                                 </includes>
> 
>                 </resource>
> 
> </resources>
> 
>  
> 
> I see in the log that it’s doing something with the file, maybe not
> what I was intending though:
> 
> [INFO] Copying 1 resource
> 
>  
> 
> Any help would be appreciative.
> 
>  
> 
>  
> 
> Thanks,
> 
>      Nick
> 
> Description:
> http://www.cisco.com/web/europe/images/email/signature/logo02.jpg
> 
> 
> Nicholas Cuneo
> Software Engineer
> SPVTG
> ncuneo@cisco.com
> Phone: 1-714-434-2344
> Mobile: 1-949-243-4952
> 
> 
> 
> 
> 
> 3500 Hyland Ave
> Costa Mesa, CA
> United States
> Cisco.com
> 
> Download
> vCardDescription:
> Virtual Card File
> 
> 
> 
> 
>  
> 
> Description: Think before you print.Think before you print.
> 
> This email may contain confidential and privileged material for the
> sole use of the intended recipient. Any review, use, distribution or
> disclosure by others is strictly prohibited. If you are not the
> intended recipient (or authorized to receive for the recipient),
> please contact the sender by reply email and delete all copies of this
> message.
> 
> For corporate legal information go to:
> http://www.cisco.com/web/about/doing_business/legal/cri/index.html
> 
>  
> 
> 
> 
> 
> 
>  
> 
>  
> 
> 

RE: Properties within source code

Posted by "Nick Cuneo (ncuneo)" <nc...@cisco.com>.
I agree this isn't the best approach, however I'm trying to do as much as possible with legacy code and trying to not make any changes to source code.

Thanks,
-Nick


-----Original Message-----
From: John Kramer [mailto:jkramer@mojiva.com] 
Sent: Friday, November 16, 2012 2:03 PM
To: Maven Users List
Subject: Re: Properties within source code

I know that this may not address your question directly, but I suggest that you change your approach.

Rather than build two different artifacts for debugging or not debugging, I suggest that you make the a runtime configuration.  Look into various logging frameworks (log4j, log back) that will allow this.  They are pretty flexible and should probably help you.

If they don't address your issue, you should look into using java system properties.  The tag <argLine> will allow you to set them in tests run with the surefire plugin.

You can also package a properties file with your code and use maven properties to filter that.  However, as far as I know, Maven only has the capability to filter resources (such as the aforementioned properties file), not java source code (and if it does have that capability, I would suggest not using it as it would screw up static source tools).


John Kramer
email: jkramer@mojiva.com<ma...@mojiva.com>
mobile: 314.435.2370
skype: kramer.mojiva
twitter: @KramerKnowsTech<https://twitter.com/KramerKnowsTech>
0xCAFEBABE00000032


From: "Nick Cuneo (ncuneo)" <nc...@cisco.com>>
Reply-To: Maven Users List <us...@maven.apache.org>>
Date: Friday, November 16, 2012 16:53 PM
To: "users@maven.apache.org<ma...@maven.apache.org>" <us...@maven.apache.org>>
Subject: Properties within source code

Hi,

We are attempting to migrate our build environment from being very Ant-centric, to be Maven-centric.  We have a source file (Trace.java) that has a couple of variables that get set for debug/non-debug builds.  The code is as follows public boolean DEBUG = ${debug.enabled};

Obviously this won't compile as is, but with Ant we were able to replace that property at compile time so it would build the appropriate package.  I'm trying to get that same functionality with Maven... I've looked into trying to add the file as a resource and creating the property in the pom.xml.  I've also tried using the google-replace plugin, but that didn't seem to work well either.  Am I just not doing it right?  I'm learning Maven as I go so I figured that's a possibility.  I've tried doing searches on the subject with google and looking through maven documentation and it feels straightforward but just not working for me.

Example from pom:
<properties>
                                <debug.enabled>true</debug.enabled>
</properties>
<build>
...
<resources>
                <resource>
                                <directory>${basedir}/src/trace</directory>
                                <includes>
                                                <include>Trace.java</include>
                                </includes>
                </resource>
</resources>

I see in the log that it's doing something with the file, maybe not what I was intending though:
[INFO] Copying 1 resource

Any help would be appreciative.


Thanks,
     Nick
[Description: http://www.cisco.com/web/europe/images/email/signature/logo02.jpg]

Nicholas Cuneo
Software Engineer
SPVTG
ncuneo@cisco.com<ma...@cisco.com>
Phone: 1-714-434-2344
Mobile: 1-949-243-4952



3500 Hyland Ave
Costa Mesa, CA
United States
Cisco.com<http://www.cisco.com/>

Download vCard[Description: Virtual Card File]<http://wwwin-tools.cisco.com/dir/vcard/ncuneo.vcf>


[Description: Think before you print.]Think before you print.
This email may contain confidential and privileged material for the sole use of the intended recipient. Any review, use, distribution or disclosure by others is strictly prohibited. If you are not the intended recipient (or authorized to receive for the recipient), please contact the sender by reply email and delete all copies of this message.
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html







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


Re: Properties within source code

Posted by Stephen Connolly <st...@gmail.com>.
There is support for generating source code from some form of template.
Usually requires a plugin for the "template" engine, or antrun and
build-helper:add-source

You would keep the "template" outside if src/main/java and then the
generated source goes to something like target/generated-source/... And
that path gets added as a source root.

But keep in mind the principle of environment agnostic artifacts. Tuning
logging levels is not such, so abusing to do such is a bad plan

On Friday, 16 November 2012, John Kramer wrote:

> I know that this may not address your question directly, but I suggest
> that you change your approach.
>
> Rather than build two different artifacts for debugging or not debugging,
> I suggest that you make the a runtime configuration.  Look into various
> logging frameworks (log4j, log back) that will allow this.  They are pretty
> flexible and should probably help you.
>
> If they don't address your issue, you should look into using java system
> properties.  The tag <argLine> will allow you to set them in tests run with
> the surefire plugin.
>
> You can also package a properties file with your code and use maven
> properties to filter that.  However, as far as I know, Maven only has the
> capability to filter resources (such as the aforementioned properties
> file), not java source code (and if it does have that capability, I would
> suggest not using it as it would screw up static source tools).
>
>
> John Kramer
> email: jkramer@mojiva.com <javascript:;><mailto:jkramer@mojiva.com<javascript:;>
> >
> mobile: 314.435.2370
> skype: kramer.mojiva
> twitter: @KramerKnowsTech<https://twitter.com/KramerKnowsTech>
> 0xCAFEBABE00000032
>
>
> From: "Nick Cuneo (ncuneo)" <ncuneo@cisco.com <javascript:;><mailto:
> ncuneo@cisco.com <javascript:;>>>
> Reply-To: Maven Users List <users@maven.apache.org <javascript:;><mailto:
> users@maven.apache.org <javascript:;>>>
> Date: Friday, November 16, 2012 16:53 PM
> To: "users@maven.apache.org <javascript:;><mailto:users@maven.apache.org<javascript:;>>"
> <users@maven.apache.org <javascript:;><mailto:users@maven.apache.org<javascript:;>
> >>
> Subject: Properties within source code
>
> Hi,
>
> We are attempting to migrate our build environment from being very
> Ant-centric, to be Maven-centric.  We have a source file (Trace.java) that
> has a couple of variables that get set for debug/non-debug builds.  The
> code is as follows
> public boolean DEBUG = ${debug.enabled};
>
> Obviously this won’t compile as is, but with Ant we were able to replace
> that property at compile time so it would build the appropriate package.
>  I’m trying to get that same functionality with Maven… I’ve looked into
> trying to add the file as a resource and creating the property in the
> pom.xml.  I’ve also tried using the google-replace plugin, but that didn’t
> seem to work well either.  Am I just not doing it right?  I’m learning
> Maven as I go so I figured that’s a possibility.  I’ve tried doing searches
> on the subject with google and looking through maven documentation and it
> feels straightforward but just not working for me.
>
> Example from pom:
> <properties>
>                                 <debug.enabled>true</debug.enabled>
> </properties>
> <build>
> …
> <resources>
>                 <resource>
>                                 <directory>${basedir}/src/trace</directory>
>                                 <includes>
>
> <include>Trace.java</include>
>                                 </includes>
>                 </resource>
> </resources>
>
> I see in the log that it’s doing something with the file, maybe not what I
> was intending though:
> [INFO] Copying 1 resource
>
> Any help would be appreciative.
>
>
> Thanks,
>      Nick
> [Description:
> http://www.cisco.com/web/europe/images/email/signature/logo02.jpg]
>
> Nicholas Cuneo
> Software Engineer
> SPVTG
> ncuneo@cisco.com <javascript:;><mailto:ncuneo@cisco.com <javascript:;>>
> Phone: 1-714-434-2344
> Mobile: 1-949-243-4952
>
>
>
> 3500 Hyland Ave
> Costa Mesa, CA
> United States
> Cisco.com<http://www.cisco.com/>
>
> Download vCard[Description: Virtual Card File]<
> http://wwwin-tools.cisco.com/dir/vcard/ncuneo.vcf>
>
>
> [Description: Think before you print.]Think before you print.
> This email may contain confidential and privileged material for the sole
> use of the intended recipient. Any review, use, distribution or disclosure
> by others is strictly prohibited. If you are not the intended recipient (or
> authorized to receive for the recipient), please contact the sender by
> reply email and delete all copies of this message.
> For corporate legal information go to:
> http://www.cisco.com/web/about/doing_business/legal/cri/index.html
>
>
>
>
>
>
>

Re: Properties within source code

Posted by John Kramer <jk...@mojiva.com>.
I know that this may not address your question directly, but I suggest that you change your approach.

Rather than build two different artifacts for debugging or not debugging, I suggest that you make the a runtime configuration.  Look into various logging frameworks (log4j, log back) that will allow this.  They are pretty flexible and should probably help you.

If they don't address your issue, you should look into using java system properties.  The tag <argLine> will allow you to set them in tests run with the surefire plugin.

You can also package a properties file with your code and use maven properties to filter that.  However, as far as I know, Maven only has the capability to filter resources (such as the aforementioned properties file), not java source code (and if it does have that capability, I would suggest not using it as it would screw up static source tools).


John Kramer
email: jkramer@mojiva.com<ma...@mojiva.com>
mobile: 314.435.2370
skype: kramer.mojiva
twitter: @KramerKnowsTech<https://twitter.com/KramerKnowsTech>
0xCAFEBABE00000032


From: "Nick Cuneo (ncuneo)" <nc...@cisco.com>>
Reply-To: Maven Users List <us...@maven.apache.org>>
Date: Friday, November 16, 2012 16:53 PM
To: "users@maven.apache.org<ma...@maven.apache.org>" <us...@maven.apache.org>>
Subject: Properties within source code

Hi,

We are attempting to migrate our build environment from being very Ant-centric, to be Maven-centric.  We have a source file (Trace.java) that has a couple of variables that get set for debug/non-debug builds.  The code is as follows
public boolean DEBUG = ${debug.enabled};

Obviously this won’t compile as is, but with Ant we were able to replace that property at compile time so it would build the appropriate package.  I’m trying to get that same functionality with Maven… I’ve looked into trying to add the file as a resource and creating the property in the pom.xml.  I’ve also tried using the google-replace plugin, but that didn’t seem to work well either.  Am I just not doing it right?  I’m learning Maven as I go so I figured that’s a possibility.  I’ve tried doing searches on the subject with google and looking through maven documentation and it feels straightforward but just not working for me.

Example from pom:
<properties>
                                <debug.enabled>true</debug.enabled>
</properties>
<build>
…
<resources>
                <resource>
                                <directory>${basedir}/src/trace</directory>
                                <includes>
                                                <include>Trace.java</include>
                                </includes>
                </resource>
</resources>

I see in the log that it’s doing something with the file, maybe not what I was intending though:
[INFO] Copying 1 resource

Any help would be appreciative.


Thanks,
     Nick
[Description: http://www.cisco.com/web/europe/images/email/signature/logo02.jpg]

Nicholas Cuneo
Software Engineer
SPVTG
ncuneo@cisco.com<ma...@cisco.com>
Phone: 1-714-434-2344
Mobile: 1-949-243-4952



3500 Hyland Ave
Costa Mesa, CA
United States
Cisco.com<http://www.cisco.com/>

Download vCard[Description: Virtual Card File]<http://wwwin-tools.cisco.com/dir/vcard/ncuneo.vcf>


[Description: Think before you print.]Think before you print.
This email may contain confidential and privileged material for the sole use of the intended recipient. Any review, use, distribution or disclosure by others is strictly prohibited. If you are not the intended recipient (or authorized to receive for the recipient), please contact the sender by reply email and delete all copies of this message.
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html