You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Søren Krogh Neigaard <So...@systematic.com> on 2010/02/04 09:59:26 UTC

Read properties from pom.xml from Java

Hi

 

I have been tossed into a maven controlled project. We run our junit tests from Eclipse and also on our build server. When ever they are run, some maven magic happens so that username/password/driver/url for the dabase is correctly set for the user pc or the buildserver. However I need to make a small helper class for my junit tests that wipes the database and loads some testdata on the database for every junit test.

 

The username/password/driver/url for the database is set in the pom.xml file by maven. How do I read these values from my Java helper class?

 

Best regards

Søren 

 


Re: Read properties from pom.xml from Java

Posted by Stephen Connolly <st...@gmail.com>.
On 5 February 2010 06:45, Søren Krogh Neigaard <
Soeren.Krogh.Neigaard@systematic.com> wrote:

> Thank you all for your answers
>
> I tried adding the following to my pom.xml
>
> <properties>
>  <database.driver>oracle.jdbc.OracleDriver</database.driver>
>  <database.url>${database.url}</database.url>
>  <database.username>${database.username}</database.username>
>  <database.password>${database.password}</database.password>
> </properties>
>
> And tried reading with System.getProperty("database.username"), but it gave
> me null.
>
>
Are you doing this from within a JUnit test launched by either
maven-surefire-plugin or maven-failsafe-plugin?

If so then the reason you cannot read the properties is because surefire is
cleaning up the system properties in order to encourage keeping your code
decoupled from maven... (which actually is a good thing... good build tools
should not lock you in, and should encourage you to do things in a way where
you are not locked into your build tool... that way you will continue to use
that build tool because it is the best, and not because you are locked in)

There are really two ways to solve your problem...

1. Read the connection details from a file on the classpath, e.g. in your
unit test you would do something like:

public class MyTest {

    ...

    private final Properties properties = new Properties();

    @Before
    public void setUp() throws IOException {
        properties.load(getClass().getResourceAsStream("/test.properties"));
        ...
    }

   ...

}

you then enabling filtering on the properties file which you put in
src/test/resources, e.g. for the above example you would put your properties
in ${basedir}/src/test/resources/test.properties

For most of my projects, I get fed up enabling filtering on specific files,
so I just have this in our corporate root pom:

        <resources>
            <resource>
                <filtering>false</filtering>

<directory>${basedir}/src/main/resources/verbatim</directory>
            </resource>
            <resource>
                <filtering>true</filtering>

<directory>${basedir}/src/main/resources/filtered</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <filtering>false</filtering>

<directory>${basedir}/src/test/resources/verbatim</directory>
            </testResource>
            <testResource>
                <filtering>true</filtering>

<directory>${basedir}/src/test/resources/filtered</directory>
            </testResource>
        </testResources>

And I would but test.properties in ${basedir}/src/test/resources/filtered

2. Read the connection details from the System properties... this requires
telling Surefire/Failsafe to set the system properties, see
http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html

HTH

-Stephen

The reason for the ${database.url} and so on, is that it gets its values
> from either a default settings.xml or a user specific settings.xml, and that
> is how I need it to be. We are already using the sql-maven-plugin to
> bootstrap the database, but it only gets run once before all junit tests are
> run, and that is not what I need, I need it to run for every test to ensure
> a clean database.
>
> I can easily make some code that drops my tables and so on, but I need the
> correct database properties from the correct settings.xml.
>
> Bernd you talked about making a plugin that gets called before every test,
> how can I do this?
>
> Med venlig hilsen / Kind regards
>
>
> Søren Krogh Neigaard
> Systems Engineer
>
> Søren Frichs Vej 39, 8000 Aarhus C
> Denmark
>
> Mobile +4541965252
> soeren.krogh.neigaard@systematic.com
> www.systematic.com
>
>
> -----Original Message-----
> From: bernd.adamowicz@external.icw-global.com [mailto:
> bernd.adamowicz@external.icw-global.com]
> Sent: 4. februar 2010 11:59
> To: Maven Users List
> Subject: Re: Read properties from pom.xml from Java
>
> Sounds like this might help you:
>
> - Create a Maven plugin which is executed in any phase >>bevore<< test,
> e.g. 'generate-test-resources'. This will make it possible to set up the
> database. This plugin might help for starting the database (only if you're
> using HSQL):
>
> http://gforge.openehealth.org/gf/project/development/wiki/?pagename=Documentation+HSQLDB+plugin
> , but there are others, too.
>
> - Reading of POM-properties inside a Maven plugin is easy and described
> here:
>
> http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-mojo-params.html#writing-plugins-sect-param-values
>
> However, the scenario described by you clearly indicates that this is not
> a unit test instead it is an integration test where other rules/plugins
> might apply, e.g. maven-invoker-plugin. But this would recommend a
> restructuring of your project and might not be what you want.
>
> Bernd
>
>
>
>
>
>
> Søren Krogh Neigaard <So...@systematic.com> wrote on
> 04.02.2010 09:59:26:
>
> > [image removed]
> >
> > Read properties from pom.xml from Java
> >
> > Søren Krogh Neigaard
> >
> > to:
> >
> > users
> >
> > 04.02.2010 10:00
> >
> > Please respond to "Maven Users List"
> >
> > Hi
> >
> >
> >
> > I have been tossed into a maven controlled project. We run our junit
> > tests from Eclipse and also on our build server. When ever they are
> > run, some maven magic happens so that username/password/driver/url
> > for the dabase is correctly set for the user pc or the buildserver.
> > However I need to make a small helper class for my junit tests that
> > wipes the database and loads some testdata on the database for every
> > junit test.
> >
> >
> >
> > The username/password/driver/url for the database is set in the
> > pom.xml file by maven. How do I read these values from my Java helper
> class?
> >
> >
> >
> > Best regards
> >
> > Søren
> >
> >
> >
>
>
>
> InterComponentWare AG:
> Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.:
> Prof. Dr. Christof Hettich
> Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 /
> USt.-IdNr.: DE 198388516  =
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: Read properties from pom.xml from Java

Posted by Stephen Connolly <st...@gmail.com>.
On 5 February 2010 10:01, Martin Höller <ma...@xss.co.at> wrote:

> Am Freitag, 5. Februar 2010 09:50:04 schrieb Søren Krogh Neigaard:
> > Im getting grey hairs :)
> >
> > I tried with this in my pom.xml:
> >
> > <plugin>
> >   <groupId>org.apache.maven.plugins</groupId>
> >   <artifactId>maven-surefire-plugin</artifactId>
> >   <version>2.5</version>
> >   <configuration>
> >     <systemPropertyVariables>
> >       <db.driver>oracle.jdbc.OracleDriver</db.driver>
> >       <db.url>${database.url}</db.url>
> >       <db.username>${database.username}</db.username>
> >       <db.password>${database.password}</db.password>
> >     </systemPropertyVariables>
> >   </configuration>
> > </plugin>
>
> That's almost exactly how I do it. The differences are:
> - I'm still using surefire 2.4.2
> - Due to the older version I use the (now) deprecated method of defining
>  system properties in surefire (<systemProperties> instead of
>  <systemPropertyVariables>)
>
> How and where are you trying to access the properties? In some JUnit test
> which is executed by the maven surefire plugin via
> System.getProperty("db.url")? That definitely works for me.
>
> However, Stephen's comments about locking you to maven this way sounds
> reasonable. His approach is probably the better one. But I don't know yet
> how
> these properties could be used in pom.xml for plugin configuration. Do they
> get imported by maven somehow?
>

My way would have test.properties with the following contents:

db.driver=oracle.jdbc.OracleDriver
db.url=${database.url}
db.username=${database.username}
db.password=${database.password}

By having the test resource filtered, the properties from the pom/settings
will be substituted into the resource when it is copied to
target/test-classes by maven-resources-plugin

If you decide to move away from maven, all you have to do is modify how the
test.properties file gets copied...

and the bonus is that you can debug the property substitution as you can
look at target/test-classes/test.properties to verify that it is being
substituted correctly.


>
> hth,
> - martin
>

RE: Read properties from pom.xml from Java

Posted by Søren Krogh Neigaard <So...@systematic.com>.
Well it seems that I use 2.4.3, so I have changed it to the now deprecated way also. It still gives me null values... :(

I think my problem is elsewhere now, I do not think maven gets activated when I run my junit tests. I will look into this issue now.

Med venlig hilsen / Kind regards 


Søren Krogh Neigaard
Systems Engineer

Søren Frichs Vej 39, 8000 Aarhus C
Denmark 

Mobile +4541965252
soeren.krogh.neigaard@systematic.com
www.systematic.com


-----Original Message-----
From: Martin Höller [mailto:martin@xss.co.at] 
Sent: 5. februar 2010 11:01
To: Maven Users List
Subject: Re: Read properties from pom.xml from Java

Am Freitag, 5. Februar 2010 09:50:04 schrieb Søren Krogh Neigaard:
> Im getting grey hairs :)
>
> I tried with this in my pom.xml:
>
> <plugin>
>   <groupId>org.apache.maven.plugins</groupId>
>   <artifactId>maven-surefire-plugin</artifactId>
>   <version>2.5</version>
>   <configuration>
>     <systemPropertyVariables>
>       <db.driver>oracle.jdbc.OracleDriver</db.driver>
>       <db.url>${database.url}</db.url>
>       <db.username>${database.username}</db.username>
>       <db.password>${database.password}</db.password>
>     </systemPropertyVariables>
>   </configuration>
> </plugin>

That's almost exactly how I do it. The differences are:
- I'm still using surefire 2.4.2
- Due to the older version I use the (now) deprecated method of defining
  system properties in surefire (<systemProperties> instead of
  <systemPropertyVariables>)

How and where are you trying to access the properties? In some JUnit test which is executed by the maven surefire plugin via System.getProperty("db.url")? That definitely works for me.

However, Stephen's comments about locking you to maven this way sounds reasonable. His approach is probably the better one. But I don't know yet how these properties could be used in pom.xml for plugin configuration. Do they get imported by maven somehow?


hth,
- martin

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


Re: Read properties from pom.xml from Java

Posted by Martin Höller <ma...@xss.co.at>.
Am Freitag, 5. Februar 2010 09:50:04 schrieb Søren Krogh Neigaard:
> Im getting grey hairs :)
>
> I tried with this in my pom.xml:
>
> <plugin>
>   <groupId>org.apache.maven.plugins</groupId>
>   <artifactId>maven-surefire-plugin</artifactId>
>   <version>2.5</version>
>   <configuration>
>     <systemPropertyVariables>
>       <db.driver>oracle.jdbc.OracleDriver</db.driver>
>       <db.url>${database.url}</db.url>
>       <db.username>${database.username}</db.username>
>       <db.password>${database.password}</db.password>
>     </systemPropertyVariables>
>   </configuration>
> </plugin>

That's almost exactly how I do it. The differences are:
- I'm still using surefire 2.4.2
- Due to the older version I use the (now) deprecated method of defining
  system properties in surefire (<systemProperties> instead of
  <systemPropertyVariables>)

How and where are you trying to access the properties? In some JUnit test 
which is executed by the maven surefire plugin via 
System.getProperty("db.url")? That definitely works for me.

However, Stephen's comments about locking you to maven this way sounds 
reasonable. His approach is probably the better one. But I don't know yet how 
these properties could be used in pom.xml for plugin configuration. Do they 
get imported by maven somehow?

hth,
- martin

RE: Read properties from pom.xml from Java

Posted by Søren Krogh Neigaard <So...@systematic.com>.
Im getting grey hairs :)

I tried with this in my pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <systemPropertyVariables>
      <db.driver>oracle.jdbc.OracleDriver</db.driver>
      <db.url>${database.url}</db.url>
      <db.username>${database.username}</db.username>
      <db.password>${database.password}</db.password>
    </systemPropertyVariables>
  </configuration>
</plugin>

And this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <systemPropertyVariables>
      <db.driver>oracle.jdbc.OracleDriver</db.driver>
      <db.url>${settings.database.url}</db.url>
      <db.username>${settings.database.username}</db.username>
      <db.password>${settings.database.password}</db.password>
    </systemPropertyVariables>
  </configuration>
</plugin>

I also tried with this:

<properties>
  <db.driver>oracle.jdbc.OracleDriver</db.driver>
  <db.url>${settings.database.url}</db.url>
...

Al of my attempts gives me null :(

Med venlig hilsen / Kind regards 


Søren Krogh Neigaard
Systems Engineer

Søren Frichs Vej 39, 8000 Aarhus C
Denmark 

Mobile +4541965252
soeren.krogh.neigaard@systematic.com
www.systematic.com


-----Original Message-----
From: Martin Höller [mailto:martin@xss.co.at] 
Sent: 5. februar 2010 09:00
To: Maven Users List
Subject: Re: Read properties from pom.xml from Java

Hi!

Am Freitag, 5. Februar 2010 08:41:02 schrieb Søren Krogh Neigaard:
> I have this in my settings.xml
>
> <settings>
>   <profiles>
>     <profile>
>       <activation>
>         <activeByDefault>true</activeByDefault>
>       </activation>
>       <properties>
>         <database.driver>oracle.jdbc.driver.OracleDriver</database.driver>
>         <database.url>jdbc:oracle:thin:@myhome:1521:orcl</database.url>
>         <database.username>foo</database.username>
>         <database.password>bar</database.password>
>       </properties>
>     </profile>
>   </profiles>
> </settings>
>
> And I have tried with these in my pom.xml:
>
> <properties>
>   <db.driver>oracle.jdbc.OracleDriver</db.driver>
>   <db.url>${database.url}</db.url>
>   <db.username>${database.username}</db.username>
>   <db.password>${database.password}</db.password>
> </properties>
>
> And this:
>
> <properties>
>   <database.driver>oracle.jdbc.OracleDriver</database.driver>
>   <database.url>dummy</database.url>
>   <database.username>dummy</database.username>
>   <database.password>dummy</database.password>
> </properties>
>
> I still get null with System.getProperty

Sorry, I missed one important point: you have to "export" your properties to become system properties. Do this by configuring the surefire plugin as described here:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html

I just double checked it, this way it works.

hth,
- martin

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


Re: Read properties from pom.xml from Java

Posted by Martin Höller <ma...@xss.co.at>.
Hi!

Am Freitag, 5. Februar 2010 08:41:02 schrieb Søren Krogh Neigaard:
> I have this in my settings.xml
>
> <settings>
>   <profiles>
>     <profile>
>       <activation>
>         <activeByDefault>true</activeByDefault>
>       </activation>
>       <properties>
>         <database.driver>oracle.jdbc.driver.OracleDriver</database.driver>
>         <database.url>jdbc:oracle:thin:@myhome:1521:orcl</database.url>
>         <database.username>foo</database.username>
>         <database.password>bar</database.password>
>       </properties>
>     </profile>
>   </profiles>
> </settings>
>
> And I have tried with these in my pom.xml:
>
> <properties>
>   <db.driver>oracle.jdbc.OracleDriver</db.driver>
>   <db.url>${database.url}</db.url>
>   <db.username>${database.username}</db.username>
>   <db.password>${database.password}</db.password>
> </properties>
>
> And this:
>
> <properties>
>   <database.driver>oracle.jdbc.OracleDriver</database.driver>
>   <database.url>dummy</database.url>
>   <database.username>dummy</database.username>
>   <database.password>dummy</database.password>
> </properties>
>
> I still get null with System.getProperty

Sorry, I missed one important point: you have to "export" your properties to 
become system properties. Do this by configuring the surefire plugin as 
described here:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html

I just double checked it, this way it works.

hth,
- martin

RE: Read properties from pom.xml from Java

Posted by Søren Krogh Neigaard <So...@systematic.com>.
Hi

I have this in my settings.xml

<settings>
  <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <database.driver>oracle.jdbc.driver.OracleDriver</database.driver>
        <database.url>jdbc:oracle:thin:@myhome:1521:orcl</database.url>
        <database.username>foo</database.username>
        <database.password>bar</database.password>
      </properties>
    </profile>
  </profiles>
</settings>

And I have tried with these in my pom.xml:

<properties>
  <db.driver>oracle.jdbc.OracleDriver</db.driver>
  <db.url>${database.url}</db.url>
  <db.username>${database.username}</db.username>
  <db.password>${database.password}</db.password>
</properties>

And this:

<properties>
  <database.driver>oracle.jdbc.OracleDriver</database.driver>
  <database.url>dummy</database.url>
  <database.username>dummy</database.username>
  <database.password>dummy</database.password>
</properties>

I still get null with System.getProperty

Med venlig hilsen / Kind regards 


Søren Krogh Neigaard
Systems Engineer

Søren Frichs Vej 39, 8000 Aarhus C
Denmark 

Mobile +4541965252
soeren.krogh.neigaard@systematic.com
www.systematic.com


-----Original Message-----
From: Martin Höller [mailto:martin@xss.co.at] 
Sent: 5. februar 2010 08:30
To: Maven Users List
Subject: Re: Read properties from pom.xml from Java

Am Freitag, 5. Februar 2010 07:45:51 schrieb Søren Krogh Neigaard:
> Thank you all for your answers
>
> I tried adding the following to my pom.xml
>
> <properties>
>   <database.driver>oracle.jdbc.OracleDriver</database.driver>
>   <database.url>${database.url}</database.url>
>   <database.username>${database.username}</database.username>
>   <database.password>${database.password}</database.password>
> </properties>
>
> And tried reading with System.getProperty("database.username"), but it 
> gave me null.
>
> The reason for the ${database.url} and so on, is that it gets its 
> values from either a default settings.xml or a user specific 
> settings.xml, and that is how I need it to be.

It's as Anders wrote already. If you want to use the same property names in settings.xml and pom.xml you have to put them in a profile, that's how I do it and it works. The profile could be <activeByDefault> if you prefer.

So your settings.xml could look like this:

<settings>
  <profiles>
    <profile>
      <id>someProf</id>
      <activation>
        <activeByDefault/>
      </activation>
      <properties>
        <database.url>someUrl</database.url>
      </properties>
    </profile>
  </profiles>
</settings>
 
And your pom.xml would be this:

<project>
  ...
  <properties>
    <database.url>someDummyValue</database.url>
  </properties>
</project>

Now reading the properties via System.getProperty("database.url") shoud work.


hth,
- martin

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


Re: Read properties from pom.xml from Java

Posted by Martin Höller <ma...@xss.co.at>.
Am Freitag, 5. Februar 2010 07:45:51 schrieb Søren Krogh Neigaard:
> Thank you all for your answers
>
> I tried adding the following to my pom.xml
>
> <properties>
>   <database.driver>oracle.jdbc.OracleDriver</database.driver>
>   <database.url>${database.url}</database.url>
>   <database.username>${database.username}</database.username>
>   <database.password>${database.password}</database.password>
> </properties>
>
> And tried reading with System.getProperty("database.username"), but it gave
> me null.
>
> The reason for the ${database.url} and so on, is that it gets its values
> from either a default settings.xml or a user specific settings.xml, and
> that is how I need it to be.

It's as Anders wrote already. If you want to use the same property names in 
settings.xml and pom.xml you have to put them in a profile, that's how I do 
it and it works. The profile could be <activeByDefault> if you prefer.

So your settings.xml could look like this:

<settings>
  <profiles>
    <profile>
      <id>someProf</id>
      <activation>
        <activeByDefault/>
      </activation>
      <properties>
        <database.url>someUrl</database.url>
      </properties>
    </profile>
  </profiles>
</settings>
 
And your pom.xml would be this:

<project>
  ...
  <properties>
    <database.url>someDummyValue</database.url>
  </properties>
</project>

Now reading the properties via System.getProperty("database.url") shoud work.


hth,
- martin

Re: Read properties from pom.xml from Java

Posted by Anders Hammar <an...@hammar.net>.
> I tried adding the following to my pom.xml
>
> <properties>
>  <database.driver>oracle.jdbc.OracleDriver</database.driver>
>  <database.url>${database.url}</database.url>
>  <database.username>${database.username}</database.username>
>  <database.password>${database.password}</database.password>
> </properties>
>

Sure, that doesn't work. The properties you want to be set in settings.xml
(either globally or locally), you just skip those in your pom.
However, you should clearly state in the pom what properties are required. I
think I would use one set of properties to be defined in settings.xml and
then reference those when setting the properties in the project. IMO that's
a little bit more clear.
Like this:

<properties>
 <database.driver>oracle.jdbc.OracleDriver</database.driver>
 <database.url>${oracle.db.url}</database.url>
 <database.username>${oracle.db.username}</database.username>
 <database.password>${oracle.db.password}</database.password>
</properties>

/Anders


> And tried reading with System.getProperty("database.username"), but it gave
> me null.
>
> The reason for the ${database.url} and so on, is that it gets its values
> from either a default settings.xml or a user specific settings.xml, and that
> is how I need it to be. We are already using the sql-maven-plugin to
> bootstrap the database, but it only gets run once before all junit tests are
> run, and that is not what I need, I need it to run for every test to ensure
> a clean database.
>
> I can easily make some code that drops my tables and so on, but I need the
> correct database properties from the correct settings.xml.
>
> Bernd you talked about making a plugin that gets called before every test,
> how can I do this?
>
> Med venlig hilsen / Kind regards
>
>
> Søren Krogh Neigaard
> Systems Engineer
>
> Søren Frichs Vej 39, 8000 Aarhus C
> Denmark
>
> Mobile +4541965252
> soeren.krogh.neigaard@systematic.com
> www.systematic.com
>
>
> -----Original Message-----
> From: bernd.adamowicz@external.icw-global.com [mailto:
> bernd.adamowicz@external.icw-global.com]
> Sent: 4. februar 2010 11:59
> To: Maven Users List
> Subject: Re: Read properties from pom.xml from Java
>
> Sounds like this might help you:
>
> - Create a Maven plugin which is executed in any phase >>bevore<< test,
> e.g. 'generate-test-resources'. This will make it possible to set up the
> database. This plugin might help for starting the database (only if you're
> using HSQL):
>
> http://gforge.openehealth.org/gf/project/development/wiki/?pagename=Documentation+HSQLDB+plugin
> , but there are others, too.
>
> - Reading of POM-properties inside a Maven plugin is easy and described
> here:
>
> http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-mojo-params.html#writing-plugins-sect-param-values
>
> However, the scenario described by you clearly indicates that this is not
> a unit test instead it is an integration test where other rules/plugins
> might apply, e.g. maven-invoker-plugin. But this would recommend a
> restructuring of your project and might not be what you want.
>
> Bernd
>
>
>
>
>
>
> Søren Krogh Neigaard <So...@systematic.com> wrote on
> 04.02.2010 09:59:26:
>
> > [image removed]
> >
> > Read properties from pom.xml from Java
> >
> > Søren Krogh Neigaard
> >
> > to:
> >
> > users
> >
> > 04.02.2010 10:00
> >
> > Please respond to "Maven Users List"
> >
> > Hi
> >
> >
> >
> > I have been tossed into a maven controlled project. We run our junit
> > tests from Eclipse and also on our build server. When ever they are
> > run, some maven magic happens so that username/password/driver/url
> > for the dabase is correctly set for the user pc or the buildserver.
> > However I need to make a small helper class for my junit tests that
> > wipes the database and loads some testdata on the database for every
> > junit test.
> >
> >
> >
> > The username/password/driver/url for the database is set in the
> > pom.xml file by maven. How do I read these values from my Java helper
> class?
> >
> >
> >
> > Best regards
> >
> > Søren
> >
> >
> >
>
>
>
> InterComponentWare AG:
> Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.:
> Prof. Dr. Christof Hettich
> Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 /
> USt.-IdNr.: DE 198388516  =
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

RE: Read properties from pom.xml from Java

Posted by be...@external.icw-global.com.
According to Maven's documentation here 
http://maven.apache.org/pom.html#Properties, this should work (I didn't 
try it):

settings.xml:

<settings>
  <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <database.driver>oracle.jdbc.driver.OracleDriver</database.driver>
        <database.url>jdbc:oracle:thin:@myhome:1521:orcl</database.url>

pom.xml:

<properties>
  <db.driver>oracle.jdbc.OracleDriver</db.driver>
  <db.url>${settings.database.url}</db.url>

I don't know if the profile comes into play in this case or if the whole 
path is needed like 'settings.profiles.profile....'. Just try it.

Bernd


Søren Krogh Neigaard <So...@systematic.com> wrote on 
05.02.2010 08:47:04:

> [image removed] 
> 
> RE: Read properties from pom.xml from Java
> 
> Søren Krogh Neigaard 
> 
> to:
> 
> Maven Users List
> 
> 05.02.2010 08:47
> 
> Please respond to "Maven Users List"
> 
> But I do use the @Before annotation, and I was planning to call my 
> database cleanup from here. But my problem is that the database 
> properties are not accessible to me (I think), and they are managed 
> by maven (are they not?).
> 
> Med venlig hilsen / Kind regards 
> 
> 
> Søren Krogh Neigaard
> Systems Engineer
> 
> Søren Frichs Vej 39, 8000 Aarhus C
> Denmark 
> 
> Mobile +4541965252
> soeren.krogh.neigaard@systematic.com
> www.systematic.com


InterComponentWare AG:  
Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.: Prof. Dr. Christof Hettich  
Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 / USt.-IdNr.: DE 198388516  

RE: Read properties from pom.xml from Java

Posted by Søren Krogh Neigaard <So...@systematic.com>.
But I do use the @Before annotation, and I was planning to call my database cleanup from here. But my problem is that the database properties are not accessible to me (I think), and they are managed by maven (are they not?).

Med venlig hilsen / Kind regards 


Søren Krogh Neigaard
Systems Engineer

Søren Frichs Vej 39, 8000 Aarhus C
Denmark 

Mobile +4541965252
soeren.krogh.neigaard@systematic.com
www.systematic.com


-----Original Message-----
From: bernd.adamowicz@external.icw-global.com [mailto:bernd.adamowicz@external.icw-global.com] 
Sent: 5. februar 2010 09:13
To: Maven Users List
Subject: RE: Read properties from pom.xml from Java

Søren Krogh Neigaard <So...@systematic.com> wrote on 
05.02.2010 07:45:51:

> We are already using 
> the sql-maven-plugin to bootstrap the database, but it only gets run
> once before all junit tests are run, and that is not what I need, I 
> need it to run for every test to ensure a clean database.
> 
> I can easily make some code that drops my tables and so on, but I 
> need the correct database properties from the correct settings.xml.
> 
> Bernd you talked about making a plugin that gets called before every
> test, how can I do this?

Søren,

after reading this post from you I know that my suggestion is not what you 
want, since you already have it (starting the sql-maven-plugin once before 
all tests). If you need something to be done before every single test, 
you'll have to use Junit's '@Before' annotated methods. If you need some 
advice here, we should make this outside this list since this is no Maven 
issue at all.

Bernd

> 
> Med venlig hilsen / Kind regards 
> 
> 
> Søren Krogh Neigaard
> Systems Engineer
> 
> Søren Frichs Vej 39, 8000 Aarhus C
> Denmark 
> 
> Mobile +4541965252
> soeren.krogh.neigaard@systematic.com
> www.systematic.com
> 
> 
> -----Original Message-----
> From: bernd.adamowicz@external.icw-global.com [
> mailto:bernd.adamowicz@external.icw-global.com] 
> Sent: 4. februar 2010 11:59
> To: Maven Users List
> Subject: Re: Read properties from pom.xml from Java
> 
> Sounds like this might help you:
> 
> - Create a Maven plugin which is executed in any phase >>bevore<< test, 
> e.g. 'generate-test-resources'. This will make it possible to set up the 

> database. This plugin might help for starting the database (only if 
you're 
> using HSQL): 
> http://gforge.openehealth.org/gf/project/development/wiki/?
> pagename=Documentation+HSQLDB+plugin
> , but there are others, too.
> 
> - Reading of POM-properties inside a Maven plugin is easy and described 
> here: 
> http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-
> sect-mojo-params.html#writing-plugins-sect-param-values
> 
> However, the scenario described by you clearly indicates that this is 
not 
> a unit test instead it is an integration test where other rules/plugins 
> might apply, e.g. maven-invoker-plugin. But this would recommend a 
> restructuring of your project and might not be what you want. 
> 
> Bernd
> 
> 
> 
> 
> 
> 
> Søren Krogh Neigaard <So...@systematic.com> wrote on 
> 04.02.2010 09:59:26:
> 
> > [image removed] 
> > 
> > Read properties from pom.xml from Java
> > 
> > Søren Krogh Neigaard 
> > 
> > to:
> > 
> > users
> > 
> > 04.02.2010 10:00
> > 
> > Please respond to "Maven Users List"
> > 
> > Hi
> > 
> > 
> > 
> > I have been tossed into a maven controlled project. We run our junit
> > tests from Eclipse and also on our build server. When ever they are 
> > run, some maven magic happens so that username/password/driver/url 
> > for the dabase is correctly set for the user pc or the buildserver. 
> > However I need to make a small helper class for my junit tests that 
> > wipes the database and loads some testdata on the database for every
> > junit test.
> > 
> > 
> > 
> > The username/password/driver/url for the database is set in the 
> > pom.xml file by maven. How do I read these values from my Java helper 
> class?
> > 
> > 
> > 
> > Best regards
> > 
> > Søren 
> > 
> > 
> > 
> 
> 
> 
> InterComponentWare AG: 
> Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / 
> Aufsichtsratsvors.: Prof. Dr. Christof Hettich 
> Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 
> 351761 / USt.-IdNr.: DE 198388516  =
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 


InterComponentWare AG:  
Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.: Prof. Dr. Christof Hettich  
Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 / USt.-IdNr.: DE 198388516  =

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


RE: Read properties from pom.xml from Java

Posted by be...@external.icw-global.com.
Søren Krogh Neigaard <So...@systematic.com> wrote on 
05.02.2010 07:45:51:

> We are already using 
> the sql-maven-plugin to bootstrap the database, but it only gets run
> once before all junit tests are run, and that is not what I need, I 
> need it to run for every test to ensure a clean database.
> 
> I can easily make some code that drops my tables and so on, but I 
> need the correct database properties from the correct settings.xml.
> 
> Bernd you talked about making a plugin that gets called before every
> test, how can I do this?

Søren,

after reading this post from you I know that my suggestion is not what you 
want, since you already have it (starting the sql-maven-plugin once before 
all tests). If you need something to be done before every single test, 
you'll have to use Junit's '@Before' annotated methods. If you need some 
advice here, we should make this outside this list since this is no Maven 
issue at all.

Bernd

> 
> Med venlig hilsen / Kind regards 
> 
> 
> Søren Krogh Neigaard
> Systems Engineer
> 
> Søren Frichs Vej 39, 8000 Aarhus C
> Denmark 
> 
> Mobile +4541965252
> soeren.krogh.neigaard@systematic.com
> www.systematic.com
> 
> 
> -----Original Message-----
> From: bernd.adamowicz@external.icw-global.com [
> mailto:bernd.adamowicz@external.icw-global.com] 
> Sent: 4. februar 2010 11:59
> To: Maven Users List
> Subject: Re: Read properties from pom.xml from Java
> 
> Sounds like this might help you:
> 
> - Create a Maven plugin which is executed in any phase >>bevore<< test, 
> e.g. 'generate-test-resources'. This will make it possible to set up the 

> database. This plugin might help for starting the database (only if 
you're 
> using HSQL): 
> http://gforge.openehealth.org/gf/project/development/wiki/?
> pagename=Documentation+HSQLDB+plugin
> , but there are others, too.
> 
> - Reading of POM-properties inside a Maven plugin is easy and described 
> here: 
> http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-
> sect-mojo-params.html#writing-plugins-sect-param-values
> 
> However, the scenario described by you clearly indicates that this is 
not 
> a unit test instead it is an integration test where other rules/plugins 
> might apply, e.g. maven-invoker-plugin. But this would recommend a 
> restructuring of your project and might not be what you want. 
> 
> Bernd
> 
> 
> 
> 
> 
> 
> Søren Krogh Neigaard <So...@systematic.com> wrote on 
> 04.02.2010 09:59:26:
> 
> > [image removed] 
> > 
> > Read properties from pom.xml from Java
> > 
> > Søren Krogh Neigaard 
> > 
> > to:
> > 
> > users
> > 
> > 04.02.2010 10:00
> > 
> > Please respond to "Maven Users List"
> > 
> > Hi
> > 
> > 
> > 
> > I have been tossed into a maven controlled project. We run our junit
> > tests from Eclipse and also on our build server. When ever they are 
> > run, some maven magic happens so that username/password/driver/url 
> > for the dabase is correctly set for the user pc or the buildserver. 
> > However I need to make a small helper class for my junit tests that 
> > wipes the database and loads some testdata on the database for every
> > junit test.
> > 
> > 
> > 
> > The username/password/driver/url for the database is set in the 
> > pom.xml file by maven. How do I read these values from my Java helper 
> class?
> > 
> > 
> > 
> > Best regards
> > 
> > Søren 
> > 
> > 
> > 
> 
> 
> 
> InterComponentWare AG: 
> Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / 
> Aufsichtsratsvors.: Prof. Dr. Christof Hettich 
> Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 
> 351761 / USt.-IdNr.: DE 198388516  =
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 


InterComponentWare AG:  
Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.: Prof. Dr. Christof Hettich  
Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 / USt.-IdNr.: DE 198388516  

RE: Read properties from pom.xml from Java

Posted by Søren Krogh Neigaard <So...@systematic.com>.
Thank you all for your answers

I tried adding the following to my pom.xml

<properties>
  <database.driver>oracle.jdbc.OracleDriver</database.driver>
  <database.url>${database.url}</database.url>
  <database.username>${database.username}</database.username>
  <database.password>${database.password}</database.password>
</properties>

And tried reading with System.getProperty("database.username"), but it gave me null.

The reason for the ${database.url} and so on, is that it gets its values from either a default settings.xml or a user specific settings.xml, and that is how I need it to be. We are already using the sql-maven-plugin to bootstrap the database, but it only gets run once before all junit tests are run, and that is not what I need, I need it to run for every test to ensure a clean database.

I can easily make some code that drops my tables and so on, but I need the correct database properties from the correct settings.xml.

Bernd you talked about making a plugin that gets called before every test, how can I do this?

Med venlig hilsen / Kind regards 


Søren Krogh Neigaard
Systems Engineer

Søren Frichs Vej 39, 8000 Aarhus C
Denmark 

Mobile +4541965252
soeren.krogh.neigaard@systematic.com
www.systematic.com


-----Original Message-----
From: bernd.adamowicz@external.icw-global.com [mailto:bernd.adamowicz@external.icw-global.com] 
Sent: 4. februar 2010 11:59
To: Maven Users List
Subject: Re: Read properties from pom.xml from Java

Sounds like this might help you:

- Create a Maven plugin which is executed in any phase >>bevore<< test, 
e.g. 'generate-test-resources'. This will make it possible to set up the 
database. This plugin might help for starting the database (only if you're 
using HSQL): 
http://gforge.openehealth.org/gf/project/development/wiki/?pagename=Documentation+HSQLDB+plugin
, but there are others, too.

- Reading of POM-properties inside a Maven plugin is easy and described 
here: 
http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-mojo-params.html#writing-plugins-sect-param-values

However, the scenario described by you clearly indicates that this is not 
a unit test instead it is an integration test where other rules/plugins 
might apply, e.g. maven-invoker-plugin. But this would recommend a 
restructuring of your project and might not be what you want. 

Bernd






Søren Krogh Neigaard <So...@systematic.com> wrote on 
04.02.2010 09:59:26:

> [image removed] 
> 
> Read properties from pom.xml from Java
> 
> Søren Krogh Neigaard 
> 
> to:
> 
> users
> 
> 04.02.2010 10:00
> 
> Please respond to "Maven Users List"
> 
> Hi
> 
> 
> 
> I have been tossed into a maven controlled project. We run our junit
> tests from Eclipse and also on our build server. When ever they are 
> run, some maven magic happens so that username/password/driver/url 
> for the dabase is correctly set for the user pc or the buildserver. 
> However I need to make a small helper class for my junit tests that 
> wipes the database and loads some testdata on the database for every
> junit test.
> 
> 
> 
> The username/password/driver/url for the database is set in the 
> pom.xml file by maven. How do I read these values from my Java helper 
class?
> 
> 
> 
> Best regards
> 
> Søren 
> 
> 
> 



InterComponentWare AG:  
Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.: Prof. Dr. Christof Hettich  
Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 / USt.-IdNr.: DE 198388516  =

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


Re: Read properties from pom.xml from Java

Posted by be...@external.icw-global.com.
Sounds like this might help you:

- Create a Maven plugin which is executed in any phase >>bevore<< test, 
e.g. 'generate-test-resources'. This will make it possible to set up the 
database. This plugin might help for starting the database (only if you're 
using HSQL): 
http://gforge.openehealth.org/gf/project/development/wiki/?pagename=Documentation+HSQLDB+plugin
, but there are others, too.

- Reading of POM-properties inside a Maven plugin is easy and described 
here: 
http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-mojo-params.html#writing-plugins-sect-param-values

However, the scenario described by you clearly indicates that this is not 
a unit test instead it is an integration test where other rules/plugins 
might apply, e.g. maven-invoker-plugin. But this would recommend a 
restructuring of your project and might not be what you want. 

Bernd






Søren Krogh Neigaard <So...@systematic.com> wrote on 
04.02.2010 09:59:26:

> [image removed] 
> 
> Read properties from pom.xml from Java
> 
> Søren Krogh Neigaard 
> 
> to:
> 
> users
> 
> 04.02.2010 10:00
> 
> Please respond to "Maven Users List"
> 
> Hi
> 
> 
> 
> I have been tossed into a maven controlled project. We run our junit
> tests from Eclipse and also on our build server. When ever they are 
> run, some maven magic happens so that username/password/driver/url 
> for the dabase is correctly set for the user pc or the buildserver. 
> However I need to make a small helper class for my junit tests that 
> wipes the database and loads some testdata on the database for every
> junit test.
> 
> 
> 
> The username/password/driver/url for the database is set in the 
> pom.xml file by maven. How do I read these values from my Java helper 
class?
> 
> 
> 
> Best regards
> 
> Søren 
> 
> 
> 



InterComponentWare AG:  
Vorstand: Peter Kirschbauer (Vors.), Jörg Stadler / Aufsichtsratsvors.: Prof. Dr. Christof Hettich  
Firmensitz: 69190 Walldorf, Industriestraße 41 / AG Mannheim HRB 351761 / USt.-IdNr.: DE 198388516  

Re: Read properties from pom.xml from Java

Posted by Karl Heinz Marbaise <ka...@soebes.de>.
Hi,


Søren Krogh Neigaard wrote:
> However I need to make a small helper class for my junit tests that wipes
> the database and loads some testdata on the database for every junit test.
have you taken a look at the  http://mojo.codehaus.org/sql-maven-plugin/
Maven-sql-plugin   and/or to the 
http://maven-plugins.sourceforge.net/maven-dbunit-plugin/
maven-dbunit-plugin   not to forget   http://www.dbunit.org/ dbUnit 
itself...

Kind regards
Karl Heinz Marbaise
-- 
View this message in context: http://old.nabble.com/Read-properties-from-pom.xml-from-Java-tp27449529p27451408.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: Read properties from pom.xml from Java

Posted by Martin Höller <ma...@xss.co.at>.
Hi!

Am Donnerstag, 4. Februar 2010 09:59:26 schrieb Søren Krogh Neigaard:
> The username/password/driver/url for the database is set in the pom.xml
> file by maven. How do I read these values from my Java helper class?

Set them in the pom.xml via properties, eg.:

<project>
  <properties>
    <database.username>nobody</database.username>
  </properties>
</project>

And access these properties in Java via 
System.getProperty("databse.username").

hth,
- martin