NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Maven and Classpath

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
Ken McWilliams
Posted via mailing list.





PostPosted: Wed Nov 17, 2010 1:34 am    Post subject: Maven and Classpath Reply with quote

Netbeans Users,

I'm pretty new to maven. This would not be an issue if I were to create
a: New Project > Java > Java Application as in the properties there is a
libraries section and from there everything is straight forward.

I am creating a stand alone spring application to test DI. So I created
an xml file and want to add it to the class path.

NB version 6.9.1

I printed out the class path using a ClassLoader Instance and it shows
my current class path as:

/home/ken/NetBeansProjects/springTest/target/classes/
/home/ken/.m2/repository/[a lot of spring jars]

Anyways I want it to pick up an xml file somewhere under my source files
folder. Any advice?
Back to top
Johannes Homuth
Posted via mailing list.





PostPosted: Wed Nov 17, 2010 8:53 am    Post subject: Maven and Classpath Reply with quote

At first, this is not a question related to netbeans, rather a question
for a java forum. So you might not get answers as quick as you could. So
next time post such questions into a java forum.

My way is to put such resources in the /src/main/resources folder. All
files located there should be copied to the root directory of your war,
ear or whatever you will have. So you can access it. Is it that what you
want?

Ken McWilliams wrote:
Quote:
Netbeans Users,

I'm pretty new to maven. This would not be an issue if I were to create
a: New Project > Java > Java Application as in the properties there is a
libraries section and from there everything is straight forward.

I am creating a stand alone spring application to test DI. So I created
an xml file and want to add it to the class path.

NB version 6.9.1

I printed out the class path using a ClassLoader Instance and it shows
my current class path as:

/home/ken/NetBeansProjects/springTest/target/classes/
/home/ken/.m2/repository/[a lot of spring jars]

Anyways I want it to pick up an xml file somewhere under my source files
folder. Any advice?




--
B.Sc. Johannes Homuth

-------------------------------------------------------
SOURCEPARK GmbH
Helmholtzstr. 2-9, Gebäude M
10587 Berlin

Tel.: +49 (0) 30 / 39 80 68 30
Fax: +49 (0) 30 / 39 80 68 39
e-mail: address-removed
www: www.sourcepark.de
------------------------------------------------------
SOURCEPARK GmbH
Sitz der Gesellschaft: Berlin / Amtsgericht Charlottenburg
HRB 80254
Geschäftsführung: Matthias Barmeier, Harald Dürr
------------------------------------------------------


Diese Email kann vertrauliche und/oder rechtlich geschützte Informationen
enthalten. Wenn sie nicht der richtige Adressat sind oder diese E-Mail irr-
tümlich erhalten haben, informieren Sie bitte sofort diesen Absender und ver-
nichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser E-Mail ist nicht gestattet.
Back to top
Johannes Homuth
Posted via mailing list.





PostPosted: Thu Nov 18, 2010 9:57 am    Post subject: Maven and Classpath Reply with quote

I can't understand you...

If you say that you have the src/main/resource folder than put the file
there.
And if there is no src/main/resource folder, than create it.

I'll give you an example:

Put a file into the src/main/resources/myXmlFolder/test.xml
Compile the application. (now you should have a target folder structure
like this
target
|_classes
|_testclasses
|_myproject.war
|_META-INF
|_myXmlFolder
|_test.xml


And then in java code (some controller of your webapp or whatever) do
something like the following
SomeWebappController.class.getResourceAsStream("/myXmlFolder/test.xml");

now you have an inputStream of the content of the file.
This will work with unit tests, executable jars, and I quess war files too.

Greetings

Ken McWilliams wrote:
Quote:
On Wed, 2010-11-17 at 09:45 +0100, Johannes Homuth wrote:

Quote:
At first, this is not a question related to netbeans, rather a question
for a java forum. So you might not get answers as quick as you could. So
next time post such questions into a java forum.

My way is to put such resources in the /src/main/resources folder. All
files located there should be copied to the root directory of your war,
ear or whatever you will have. So you can access it. Is it that what you
want?


Well if I was creating a web application, I would have no issue doing
what you asked...

If I create a Netbeans: New Project > Maven > Maven Web application,
then I have the following folders:
Wep Pages,
Source Packages
Test Packages
Other Sources (this will hold a folder /src/main/resources)
Libraries
Test Libraries
Project Files

However...
I wanted to do some testing outside of a web container/application
server and so just set up a New Project > Maven > Maven Project.

In doing this I loose "Web Pages" of course but I also loose "Other
Sources" which previously provided me with a convenient place to put xml
files.

As shown previously, unless I move the xml file into
/home/ken/NetBeansProjects/springTest/target/classes/
which would require use of the file system and not the NB editor...
it's kind of ugly, but works...

Now if I were to create the project as a New Project > Java > Java
Application setting the class path is very straight forward.

Perhaps this is a Maven issue but I can't see this being a Java language
issue... NB provides what I need in a rather obvious way with both a
Maven Web application and a standard Java Application but not as far as
I can see with a "Maven Project".

I take your point this might be the wrong place.


Quote:
Ken McWilliams wrote:

Quote:
Netbeans Users,

I'm pretty new to maven. This would not be an issue if I were to create
a: New Project > Java > Java Application as in the properties there is a
libraries section and from there everything is straight forward.

I am creating a stand alone spring application to test DI. So I created
an xml file and want to add it to the class path.

NB version 6.9.1

I printed out the class path using a ClassLoader Instance and it shows
my current class path as:

/home/ken/NetBeansProjects/springTest/target/classes/
/home/ken/.m2/repository/[a lot of spring jars]

Anyways I want it to pick up an xml file somewhere under my source files
folder. Any advice?









--
B.Sc. Johannes Homuth

-------------------------------------------------------
SOURCEPARK GmbH
Helmholtzstr. 2-9, Gebäude M
10587 Berlin

Tel.: +49 (0) 30 / 39 80 68 30
Fax: +49 (0) 30 / 39 80 68 39
e-mail: address-removed
www: www.sourcepark.de
------------------------------------------------------
SOURCEPARK GmbH
Sitz der Gesellschaft: Berlin / Amtsgericht Charlottenburg
HRB 80254
Geschäftsführung: Matthias Barmeier, Harald Dürr
------------------------------------------------------


Diese Email kann vertrauliche und/oder rechtlich geschützte Informationen
enthalten. Wenn sie nicht der richtige Adressat sind oder diese E-Mail irr-
tümlich erhalten haben, informieren Sie bitte sofort diesen Absender und ver-
nichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser E-Mail ist nicht gestattet.
Back to top
Ken McWilliams
Posted via mailing list.





PostPosted: Thu Nov 18, 2010 10:19 pm    Post subject: Maven and Classpath Reply with quote

On Thu, 2010-11-18 at 10:22 +0100, Johannes Homuth wrote:
Quote:
I can't understand you...

If you say that you have the src/main/resource folder than put the file
there.
And if there is no src/main/resource folder, than create it.

Yes the default NB Maven web application has the src/main/resource
folder however as I mentioned a straight forward Maven application does
not.

So if I create src/main/resource folder then I get a new folder in my
Application, great however my class path is still the same. How do I
change the class path to make use of this folder?

To be clear my class path is currently (for the standard NB Maven
Application):
/home/ken/NetBeansProjects/springTest/target/classes/
/home/ken/.m2/repository/[what ever jars are in my Libraries folder]

While in the default MB maven web app the class path is reported :
/home/ken/Applications/glassfish-3.0.1/glassfish/modules/glassfish.jar
/home/ken/Applications/glassfish-3.0.1/glassfish/lib/monitor/btrace-agent.jar

So since my class path is not pointing at a Jar it isn't going to start
looking in a Jar. Further there is a jar produced however it does not
include any of the files placed under the src/main/resource folder I
created.

Okay as an example moving my xml file out
of /NetBeansProjects/springTest/target/classes/ (which works)and into
/NetBeansProjects/springTest/src/main/resource/META-INF/myXmlFolder/mySpring.xml

Will currently require
SomeWebappController.class.getResourceAsStream("./../../src/main/resource/META-INF/myXmlFolder/test.xml");

Which lets me see the xml file because it now will show up in a folder
where before I could only see it in the file system, so that is a plus
but now my path is pretty crazy.

To get the paths I just run the following snippit:
/** START **/
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

//Get the URLs
URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();

for (int i = 0; i < urls.length; i++) {
System.out.println(urls.getFile());
//out.println(urls[i].getFile() + "<br/>"); //If in a Servlet
}
/** END **/

Okay I guess I would like to know:
1) How to a add to the class path in a maven project?
2) How do I change the build to include new folders into the resulting
Jar?

Until this moment I though there might be an easy way to use the NB Gui
to achieve these things so I asked here. But I better figure out how to
achieve this from the command line.

Note these issues don't come up when creating a standard NB Maven Web
Application because the web applications Jar is on the class path and I
create new folders under the resources folder... So everything is
straight forward.

Quote:
I'll give you an example:

Put a file into the src/main/resources/myXmlFolder/test.xml
Compile the application. (now you should have a target folder structure
like this
target
|_classes
|_testclasses
|_myproject.war
|_META-INF
|_myXmlFolder
|_test.xml


And then in java code (some controller of your webapp or whatever) do
something like the following
SomeWebappController.class.getResourceAsStream("/myXmlFolder/test.xml");

now you have an inputStream of the content of the file.
This will work with unit tests, executable jars, and I quess war files too.

Greetings

Ken McWilliams wrote:
Quote:
On Wed, 2010-11-17 at 09:45 +0100, Johannes Homuth wrote:

Quote:
At first, this is not a question related to netbeans, rather a question
for a java forum. So you might not get answers as quick as you could. So
next time post such questions into a java forum.

My way is to put such resources in the /src/main/resources folder. All
files located there should be copied to the root directory of your war,
ear or whatever you will have. So you can access it. Is it that what you
want?


Well if I was creating a web application, I would have no issue doing
what you asked...

If I create a Netbeans: New Project > Maven > Maven Web application,
then I have the following folders:
Wep Pages,
Source Packages
Test Packages
Other Sources (this will hold a folder /src/main/resources)
Libraries
Test Libraries
Project Files

However...
I wanted to do some testing outside of a web container/application
server and so just set up a New Project > Maven > Maven Project.

In doing this I loose "Web Pages" of course but I also loose "Other
Sources" which previously provided me with a convenient place to put xml
files.

As shown previously, unless I move the xml file into
/home/ken/NetBeansProjects/springTest/target/classes/
which would require use of the file system and not the NB editor...
it's kind of ugly, but works...

Now if I were to create the project as a New Project > Java > Java
Application setting the class path is very straight forward.

Perhaps this is a Maven issue but I can't see this being a Java language
issue... NB provides what I need in a rather obvious way with both a
Maven Web application and a standard Java Application but not as far as
I can see with a "Maven Project".

I take your point this might be the wrong place.


Quote:
Ken McWilliams wrote:

Quote:
Netbeans Users,

I'm pretty new to maven. This would not be an issue if I were to create
a: New Project > Java > Java Application as in the properties there is a
libraries section and from there everything is straight forward.

I am creating a stand alone spring application to test DI. So I created
an xml file and want to add it to the class path.

NB version 6.9.1

I printed out the class path using a ClassLoader Instance and it shows
my current class path as:

/home/ken/NetBeansProjects/springTest/target/classes/
/home/ken/.m2/repository/[a lot of spring jars]

Anyways I want it to pick up an xml file somewhere under my source files
folder. Any advice?









--
B.Sc. Johannes Homuth

-------------------------------------------------------
SOURCEPARK GmbH
Helmholtzstr. 2-9, Gebäude M
10587 Berlin

Tel.: +49 (0) 30 / 39 80 68 30
Fax: +49 (0) 30 / 39 80 68 39
e-mail: [i]address-removed

www: www.sourcepark.de
------------------------------------------------------
SOURCEPARK GmbH
Sitz der Gesellschaft: Berlin / Amtsgericht Charlottenburg
HRB 80254
Geschäftsführung: Matthias Barmeier, Harald Dürr
------------------------------------------------------


Diese Email kann vertrauliche und/oder rechtlich geschützte Informationen
enthalten. Wenn sie nicht der richtige Adressat sind oder diese E-Mail irr-
tümlich erhalten haben, informieren Sie bitte sofort diesen Absender und ver-
nichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser E-Mail ist nicht gestattet.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo