Load a file at Lift boot?

109 views
Skip to first unread message

naasking

unread,
Mar 27, 2012, 10:14:38 PM3/27/12
to Lift
I have a CSV file I want to load at boot of my Lift app for some
initialization purposes, but I can't figure out where to place this
file. I've tried putting it under src\main\resources and then using
LiftRules.getResource("/foo.csv"), but no dice (I get Empty, so no
file).

What's the standard way to do this in Lift?

Sandro

Diego Medina

unread,
Mar 27, 2012, 10:27:59 PM3/27/12
to lif...@googlegroups.com
Are you going to pack thi file in your war, which will then be
deployed to your container? or do you think that people will have this
file on other paths and after they started your app, they may want to
restart it and have this new csv file loaded?

What I'm thinking is that you pass somethign like -Dpath.to.my.csv to
jetty and then read the property from Boot

Regards,

Diego

> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Diego Medina

unread,
Mar 27, 2012, 10:34:06 PM3/27/12
to lif...@googlegroups.com
and during development, and if you are using sbt 0.11.x you can pas
properties like this:

runtimeOptions in Runtime += Runtime.Setup( () =>
System.setProperty("my.path.to.file", "/Users/Me/file.csv"))

(I'm not sure of the exact sbt syntax, I know that for test mode I use:

testOptions in Test += Tests.Setup( () =>
System.setProperty("run.mode", "test"))

Regards,

Diego

Robert Marcano

unread,
Mar 27, 2012, 10:56:37 PM3/27/12
to lif...@googlegroups.com
Adding files src/main/resources add those files to you application
jar/war as Java resources. Checking the default implementation of
LiftRules.getResource, It try first from ServletContext.getResource if
not found getClass.getResource. You are adding the / prefix so it must
locate it with the second try?

Are you sure the file is being packaged? check inside the resulting
war WEB-INF/classes

> -
--
Robert Marcano

naasking

unread,
Mar 27, 2012, 10:58:29 PM3/27/12
to Lift
I'm just looking for the simplest solution. There's only one csv file,
so I don't need to support multiple paths. If there's a simple way to
embed it in the WAR and load it in Lift, that'd be great.

Sandro

Robert Marcano

unread,
Mar 27, 2012, 11:01:24 PM3/27/12
to lif...@googlegroups.com
On Tue, Mar 27, 2012 at 9:57 PM, Diego Medina <di...@fmpwizard.com> wrote:
> Are you going to pack thi file in your war, which will then be
> deployed to your container? or do you think that people will have this
> file on other paths and after they started your app, they may want to
> restart it and have this new csv file loaded?
>
> What I'm thinking is that you pass somethign like -Dpath.to.my.csv  to
> jetty and then read the property from Boot

Not saying that this does not work, but I always encourage people to
no use system properties. More that one time I have been unable to
install two instances of the same war on the same VM because people
use one System property

I prefer people use the standard web.xml/context-param setting, this
way it is available to see to any deployer and documented that that
setting exists

--
Robert Marcano

Antonio Salazar Cardozo

unread,
Mar 27, 2012, 11:13:10 PM3/27/12
to lif...@googlegroups.com
I'd also try dropping the leading /. I don't think I've needed it in the past.
Thanks,
Antonio

Robert Marcano

unread,
Mar 27, 2012, 11:19:54 PM3/27/12
to lif...@googlegroups.com
On Tue, Mar 27, 2012 at 10:43 PM, Antonio Salazar Cardozo
<savedf...@gmail.com> wrote:
> I'd also try dropping the leading /. I don't think I've needed it in the
> past.
> Thanks,
> Antonio

in LiftRules:

private val defaultFinder = getClass.getResource _

getClass.getResource needs the / prefix if not it will look for the
file inside net/liftweb/http (getClass points to LiftRules)

naasking

unread,
Mar 27, 2012, 11:29:52 PM3/27/12
to Lift
I'm still not quite clear on what I need to do. Sounds like putting it
under src/main/resources is the right solution, but running
container:start in sbt doesn't pick up this. Perhaps I'm missing an
sbt config option to package this file properly?

You might have to be a little more explicit since I'm not as familiar
with Java web deployments. It's quite different from .NET. ;-)

Sandro

On Mar 27, 11:19 pm, Robert Marcano <rob...@marcanoonline.com> wrote:
> On Tue, Mar 27, 2012 at 10:43 PM, Antonio Salazar Cardozo
>
> <savedfastc...@gmail.com> wrote:
> > I'd also try dropping the leading /. I don't think I've needed it in the
> > past.
> > Thanks,
> > Antonio
>
> in LiftRules:
>
>      private val defaultFinder = getClass.getResource _
>
> getClass.getResource needs the / prefix if not it will look for the
> file inside net/liftweb/http (getClass points to LiftRules)
>
>
>
>
>
>
>
>
>
>
>
> > On Tuesday, March 27, 2012 10:56:37 PM UTC-4, Robert Marcano wrote:
>
> >> Adding files src/main/resources add those files to you application
> >> jar/war as Java resources. Checking the default implementation of
> >> LiftRules.getResource, It try first from ServletContext.getResource if
> >> not found getClass.getResource. You are adding the / prefix so it must
> >> locate it with the second try?
>
> >> Are you sure the file is being packaged? check inside the resulting
> >> war WEB-INF/classes
>

naasking

unread,
Mar 28, 2012, 1:23:21 AM3/28/12
to Lift
Ok, LiftRules.getResource("/foo.csv") worked after I *manually* copied
foo.csv into target/scala-2.9.1/classes. It's not being copied over
automatically as I expected though. At least, I've read online that
sbt is supposed to do this automatically.

However, if I first run 'sbt package', then the file is copied over
properly. It seems that sbt compile and container:start does not copy
the required resources over. Is this the expected behaviour?

Sandro

Naftoli Gugenheim

unread,
Mar 28, 2012, 1:34:42 AM3/28/12
to lif...@googlegroups.com


On Wednesday, March 28, 2012, naasking wrote:
Ok, LiftRules.getResource("/foo.csv") worked after I *manually* copied
foo.csv into target/scala-2.9.1/classes. It's not being copied over
automatically as I expected though. At least, I've read online that
sbt is supposed to do this automatically.

However, if I first run 'sbt package', then the file is copied over
properly. It seems that sbt compile and container:start does not copy
the required resources over. Is this the expected behaviour?

Ask on the sbt mailing list, and/or file an issue: https://github.com/siasia/xsbt-web-plugin/issues
 

Sandro

On Mar 27, 11:29 pm, naasking <naask...@gmail.com> wrote:
> I'm still not quite clear on what I need to do. Sounds like putting it
> under src/main/resources is the right solution, but running
> container:start in sbt doesn't pick up this. Perhaps I'm missing an
> sbt config option to package this file properly?
>
> You might have to be a little more explicit since I'm not as familiar
> with Java web deployments. It's quite different from .NET. ;-)
>
> Sandro
>
> On Mar 27, 11:19 pm, Robert Marcano <rob...@marcanoonline.com> wrote:
>
>

Sergey Trofimov

unread,
Mar 28, 2012, 3:02:18 AM3/28/12
to lif...@googlegroups.com
Hello Sandro.

You can try to do following:

1. Put your csv-file into /src/main/webapp

2. Load it with LiftRules.loadResourceAsString (or loadResource if you prefer to get binary data)

For file with name
/src/main/webapp/boot.csv the command will be LiftRules.loadResourceAsString("/boot.csv"). Leading slash is necessary.

Good luck.


--
Sergey Trofimov


Naftoli Gugenheim

unread,
Mar 27, 2012, 10:37:49 PM3/27/12
to lif...@googlegroups.com
Not sure why it doesn't work. What happens without the "/"? What about getClass.getResource?
Where is the file copied to in target/ and the .war?


On Tue, Mar 27, 2012 at 10:14 PM, naasking <naas...@gmail.com> wrote:

Richard Dallaway

unread,
Mar 29, 2012, 4:00:47 PM3/29/12
to lif...@googlegroups.com
I'm using SBT 0.11.2, and put foo.csv in src/main/resources and in Boot.scala added:
 
 println("Getting resource:")
 println( LiftRules.getResource("/foo.csv") )  

I then just ran container:start and saw:

Getting resource:
Full(file:/Users/richard/Downloads/d6y-lift_24_sbt-352b04a/target/scala-2.9.1/classes/foo.csv)

I ran package in SBT and checked the WAR to see the file was there:

$ jar tf target/scala-2.9.1/liftbasicexample_2.9.1-0.1-SNAPSHOT.war | grep foo
WEB-INF/classes/foo.csv

I also did a package after clean and clean-files and the foo.csv file was copied.

I have this version of the web plugin:

resolvers += "Web plugin repo" at "http://siasia.github.com/maven2"

libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % (v+"-0.2.11"))


Hope that's some help
Richard

Reply all
Reply to author
Forward
0 new messages