You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Rex Huang <re...@gmail.com> on 2008/01/22 16:21:17 UTC

how to replace a java file

I want to use filter to generate a src/main/resources/filter/hello.java
to replace the old one in src/main/java/com/my-company/hello.java
<resource> in pom.xml
      <resource>
        <directory>src/main/resources/filter/</directory>
        <filtering>true</filtering>
        <includes>
            <include>hello.java</include>
        </includes>
        <targetPath>com/my-company/</targetPath>
      </resource>

the result is:
hello.class is compiled using the old hello.java,
the new hello.java source file is in the jar file

does anyone knows how to do it?

BR//Rex

Re: how to replace a java file

Posted by Rex Huang <re...@gmail.com>.
On Jan 28, 2008 2:22 PM, Rex Huang <re...@gmail.com> wrote:

> when I doing a migration project, I found that many places using filter
> and replace to replace java in ant scripts, so It's necessary to have a
> plugin to replace java code.
>
> Does anyone knows how to do it?
>
> BR//Rex
>
> On Jan 22, 2008 5:50 PM, Wayne Fay <wa...@gmail.com> wrote:
>
> > When Maven builds a jar, it writes a file named pom.properties to:
> > /META-INF/maven/<groupId>/<artifactId>/pom.properties
> >
> > It looks like this:
> > #Generated by Maven
> > #Thu Sep 13 15:43:41 PDT 2007
> > version=2.0
> > groupId=org.apache.maven.plugins
> > artifactId=maven-ant-plugin
> >
> > You can just read this file directly to access the version, using
> > ClassLoader.getResourceAsStream(). Or you can create your own
> > properties file somewhere else and filter it, as suggested by
> > Heinrich.
> >
> > Wayne
> >
> > On 1/22/08, Heinrich Nirschl <he...@gmail.com> wrote:
> > > On Jan 22, 2008 5:38 PM, Rex Huang <re...@gmail.com> wrote:
> > > > for example, I had a Version.java
> > > > public class Version {
> > > >     private static String VERSION = "${buildNumber}";
> > > >     public static String getVersion() {
> > > >         return VERSION;
> > > >     }
> > > > }
> > > >
> > > > I need to set version number when compile, so I use filter to change
> > the
> > > > version information in java file.
> > > >
> > > > Rex
> > >
> > > The easiest way is to do the filtering with a real resource (e.g. a
> > > property file) and to read this resource in your java code.
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > > For additional commands, e-mail: users-help@maven.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>

Re: how to replace a java file

Posted by Rex Huang <re...@gmail.com>.
when I doing a migration project, I found that many places using filter and
replace to replace java in ant scripts, so It's necessary to have a plugin
to replace java code.

Does anyone knows how to do it?

BR//Rex

On Jan 22, 2008 5:50 PM, Wayne Fay <wa...@gmail.com> wrote:

> When Maven builds a jar, it writes a file named pom.properties to:
> /META-INF/maven/<groupId>/<artifactId>/pom.properties
>
> It looks like this:
> #Generated by Maven
> #Thu Sep 13 15:43:41 PDT 2007
> version=2.0
> groupId=org.apache.maven.plugins
> artifactId=maven-ant-plugin
>
> You can just read this file directly to access the version, using
> ClassLoader.getResourceAsStream(). Or you can create your own
> properties file somewhere else and filter it, as suggested by
> Heinrich.
>
> Wayne
>
> On 1/22/08, Heinrich Nirschl <he...@gmail.com> wrote:
> > On Jan 22, 2008 5:38 PM, Rex Huang <re...@gmail.com> wrote:
> > > for example, I had a Version.java
> > > public class Version {
> > >     private static String VERSION = "${buildNumber}";
> > >     public static String getVersion() {
> > >         return VERSION;
> > >     }
> > > }
> > >
> > > I need to set version number when compile, so I use filter to change
> the
> > > version information in java file.
> > >
> > > Rex
> >
> > The easiest way is to do the filtering with a real resource (e.g. a
> > property file) and to read this resource in your java code.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: how to replace a java file

Posted by Wayne Fay <wa...@gmail.com>.
When Maven builds a jar, it writes a file named pom.properties to:
/META-INF/maven/<groupId>/<artifactId>/pom.properties

It looks like this:
#Generated by Maven
#Thu Sep 13 15:43:41 PDT 2007
version=2.0
groupId=org.apache.maven.plugins
artifactId=maven-ant-plugin

You can just read this file directly to access the version, using
ClassLoader.getResourceAsStream(). Or you can create your own
properties file somewhere else and filter it, as suggested by
Heinrich.

Wayne

On 1/22/08, Heinrich Nirschl <he...@gmail.com> wrote:
> On Jan 22, 2008 5:38 PM, Rex Huang <re...@gmail.com> wrote:
> > for example, I had a Version.java
> > public class Version {
> >     private static String VERSION = "${buildNumber}";
> >     public static String getVersion() {
> >         return VERSION;
> >     }
> > }
> >
> > I need to set version number when compile, so I use filter to change the
> > version information in java file.
> >
> > Rex
>
> The easiest way is to do the filtering with a real resource (e.g. a
> property file) and to read this resource in your java code.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

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


Re: how to replace a java file

Posted by Heinrich Nirschl <he...@gmail.com>.
On Jan 22, 2008 5:38 PM, Rex Huang <re...@gmail.com> wrote:
> for example, I had a Version.java
> public class Version {
>     private static String VERSION = "${buildNumber}";
>     public static String getVersion() {
>         return VERSION;
>     }
> }
>
> I need to set version number when compile, so I use filter to change the
> version information in java file.
>
> Rex

The easiest way is to do the filtering with a real resource (e.g. a
property file) and to read this resource in your java code.

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


Re: how to replace a java file

Posted by Rex Huang <re...@gmail.com>.
for example, I had a Version.java
public class Version {
    private static String VERSION = "${buildNumber}";
    public static String getVersion() {
        return VERSION;
    }
}

I need to set version number when compile, so I use filter to change the
version information in java file.

Rex

On Jan 22, 2008 4:43 PM, <ni...@planet.nl> wrote:

> This is not going to work. There is a difference between sources and
> resources. And filtering is also not what you want.
>
> Resources are copied to target/classes and never compiled. Filtering is
> used to replace ${variable} kind of things.
>
> This is not easy to accomplish. Maybe you could first answer a question:
> why are you trying to accomplish this?
>
> Hth,
>
> Nick Stolwijk
>
>
> -----Original Message-----
> From: Rex Huang [mailto:rex.huang936@gmail.com]
> Sent: Tue 1/22/2008 4:21 PM
> To: users@maven.apache.org
> Subject: how to replace a java file
>
> I want to use filter to generate a src/main/resources/filter/hello.java
> to replace the old one in src/main/java/com/my-company/hello.java
> <resource> in pom.xml
>      <resource>
>        <directory>src/main/resources/filter/</directory>
>        <filtering>true</filtering>
>        <includes>
>            <include>hello.java</include>
>        </includes>
>        <targetPath>com/my-company/</targetPath>
>      </resource>
>
> the result is:
> hello.class is compiled using the old hello.java,
> the new hello.java source file is in the jar file
>
> does anyone knows how to do it?
>
> BR//Rex
>
>

RE: how to replace a java file

Posted by ni...@planet.nl.
This is not going to work. There is a difference between sources and resources. And filtering is also not what you want.

Resources are copied to target/classes and never compiled. Filtering is used to replace ${variable} kind of things.

This is not easy to accomplish. Maybe you could first answer a question: why are you trying to accomplish this?

Hth,

Nick Stolwijk


-----Original Message-----
From: Rex Huang [mailto:rex.huang936@gmail.com]
Sent: Tue 1/22/2008 4:21 PM
To: users@maven.apache.org
Subject: how to replace a java file
 
I want to use filter to generate a src/main/resources/filter/hello.java
to replace the old one in src/main/java/com/my-company/hello.java
<resource> in pom.xml
      <resource>
        <directory>src/main/resources/filter/</directory>
        <filtering>true</filtering>
        <includes>
            <include>hello.java</include>
        </includes>
        <targetPath>com/my-company/</targetPath>
      </resource>

the result is:
hello.class is compiled using the old hello.java,
the new hello.java source file is in the jar file

does anyone knows how to do it?

BR//Rex