Add classpath to folder containing property files in Maven - classpath

I have a property file which is not located in a classpath in Maven.
Therefore I can't reach it with:
ClassLoader.getSystemClassLoader().getResourceAsStream(PROPS_FILE);
How can I add the folder containing the property file to the classpath, so it will be available during build and test of the project?

Just add the file into the resources folder under src/main maven project. i did that and works like a charm.
hope it helps

Under the task you can add a set of resources and testResources like so:
<resources>
<resource>
<directory>somedir</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>test/unit</directory>
</testResource>
</testResources>
They also allow you to define exclusion and inclusion rules. This is very powerful for a legacy code base but for new code bases, you should follow the maven's standard directory layout to avoid lots of custom definitions in your POM files.

The best is to put that file under correct location in Maven like either src/main/resources and/or src/test/resources depending where it will be needed.

If you really don't like Maven's enforcement of certain directories, use the build-helper plugin to add your own directory to the classpath as generated by "mvn eclipse:eclipse".

Related

How to only include some files on a Eclipse project?

I would like to create an Eclipse project from an existing project made with IAR Workbench.
I have a list of files I want to build, but I don't know how I can tell Eclipse to only include some files only.
I've discovered that I can "exclude" a file by clicking on the file then choosing Resource Configuration/Exclude. I've also found that I can place some filters on Resource Filters, but I have found where I can just include some paths such as
${workspace_loc:/${ProjName}/Foo/Bar/foo.c
How is it possible to do this?
I am going to answer my own question: you can't.
What you can do is to list the files on your Git repository and the files that should be excluded from your Eclipse project. Then you make the difference between both list to get the list of file that are on Eclipse.
Then from the .cproject file you can edit the key
<storageModule>
<!-- ... -->
<configuration>
<!-- ... -->
<sourceEntries>
<entry excluding="{{file}}|{{file}}..."
</sourceEntries>
</configuration>
</storageModule>

Eclipse: Change webapp folder in Maven pom.xml file

Is it possible to define a different location for the webapp folder than the standard one ("/src/main/webapp/") in pom.xml? I know that you can define your web folder with
<wb-resource deploy-path="/" source-path="/webapp"/>
in a file called "org.eclipse.wst.common.component".
The problem is when I click Maven -> Update Project, this line is overwritten with the standard
<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
And then I have problems testing my project with Tomcat7 within Eclipse.
I'm very thankful for every hint.
Answers in How to configure custom maven project structure are sufficient for a purely Maven build, i.e. from commandline. When import the project into Eclipse (via m2e), you need tell m2e a little bit more so that it can create and maintain the project structure properly within Eclipse.
Actually, from Eclipse's perspective, it doesn't really care about how your project folder structure looks like, as long as the webapp folder is declared as a source folder or inside a source folder, however, by modifying .classpath doesn't make much sense as it's a auto-generated file and changed quite often.
It is highly recommended to obey the convention if you are able to, if not, using the following configuration for your customization:
<build>
<resources>
<resource>
<directory>${basedir}/webapp</directory>
</resource>
... ...
</resources>
... ...
</build>

Eclipse - Wicket - HTML files from dependent projects not being found

I want to build a reusable Wicket component in Eclipse. I have one project "components" which have files such as MyComponent.java and MyComponent.html. Then I have a project "application" which contains my application code, from which I wish to use a MyComponent.
However, Wicket cannot find the MyComponent.html. If I copy this file from "components" to "application", and use exactly the same path, then Wicket finds it no problem.
I therefore summize that Eclipse is not copying the HTML file from the dependent project "components" and making it available to the web application. I cannot really confirm that as I don't know where the JAR is being generated from the "components" project, nor do I know where/if the WAR is being generated from the "application" project.
I have looked at the project settings in "components" and cannot find any option to explicitly publish HTML (non-Java) files when the project is being built; but I cannot find any option which is explicitly forbidding this either. In the "application" project I find no option to include HTML files from the other project (there is only the option to include the JAR - which potentially should be enough?)
Update: I am not using Maven, just using the default build process of Eclipse.
Update: I am using Tomcat within Eclipse (but without any Eclipse plug-in for Tomcat; it seems to work fine without it - only obviously that's not quite true hence my question...)
Check Eclipse's source folders inclusion/exclusion filters. Project -> right button -> Properties -> Java Build path -> tab Source -> select Source Folder -> button Edit.
I'm assuming you're using Tomcat - during testing I normally use a Tomcat context to reference my Eclipse project workspace.
The workspace contains a context/WEB-INF directory structure, into which all my compiled classes, properties, HTML and other resources are copied.
The Tomcat context file lives in the directory (Tomcat)/conf/Catalina/localhost and contains an entry of the following format:
<Context path="/mywebapp" docBase="C:/eclipse/workspace/myapp/context" reloadable="true">
OK - Classic Eclipse action - for other reasons (restarting the project always resulted in a 404 for no apparent reason: I checked all the config files and everything seemed fine..), I deleted the "application" project from Eclipse and re-created it. Now everything works fine (HTML files are available...)
I had the same problem! After some time doing research I had a solution!
You need to specify to maven that it needs to include all the files, the way how maven understand this is by adding the next command.
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
It worked for me, I hope it works for any of you that have the same problem!
I guess for the person that posted this query its too late, but not for you that have this problem!

Can I substitute Eclipse and Maven variables into a log4j.properties file?

You can use system properties in log4j configuration files using a ${variablename} syntax.
Can you include Eclipse variables (like the project name) and Maven variables (like the artifact ID) in there too, and have them substituted during the respective build?
Can you include Eclipse variables (like the project name) and Maven variables (like the artifact ID) in there too, and have them substituted during the respective build?
For the later (Maven variables), you can use resources filtering. Activate it by adding a <filtering> element to your POM and setting it to true:
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
And any Maven property like ${project.artifactId} used in a resource file will now get replaced by its value. You can define includes/excludes for finer control of resources that you want to filter. Refer to the above link for examples.
For the former (Eclipse variables), Maven is not aware of them so, obviously, this solution won't work and I actually suggest sticking to Maven filtering (the Maven build should be the reference).
If you are using m2eclipse, this will work transparently inside Eclipse.
See also
How do I filter resource files?
Chapter 9. Properties and Resource Filtering
MavenPropertiesGuide

How to add external library properly in Eclipse?

So today I downloaded Apache Commons Lang library (binary, zip format). I extracted it to C:\eclipse\commons-lang-2.5 folder. There are a commons-lang-2.5.jar, a commons-lang-2.5-javadoc.jar, and a commons-lang-2.5-sources.jar inside, and a folder for HTML Javadoc. I started Eclipse, added commons-lang-2.5.jar, and set its source and Javadoc respectively as the screenshot below. (http://img43.imageshack.us/img43/9378/eclipsev.png)
My question is, is there a convenient or standard way to add external libraries? Or am I actually doing the right thing?
Recommendation:
Create a "lib" folder and keep all your jars in the folder.
Subsequently, add all the jar files in the lib folder into your build path by using Project => Properties => Java Build Path => Libraries => Add JAR ...
btw, there' no screenshot. Can you give the link for the screen shot so that I may be able to help our better...
You must add jar file on lib folder
and then right click on jar file and click "build path"-->add to build path
and now you can write that jar file code
Use maven
You don't have to download all jar's into a folder by yourself! -
use maven. It's based on a public repository, and you manage your dependencies in an xml file.
Your project will have a pom.xml file that going to look like this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
and in this file you manage the external library dependencies
for instance, if you wish to add this dependency - http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4
you will need to modify your pom.xml like so:
<project>
...
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
...
</project>
and save it. that's it
Read this getting started tutorial - https://maven.apache.org/guides/getting-started/
eclipse plugin - https://maven.apache.org/plugins/maven-eclipse-plugin
As the question is quite general, maybe following detailed answer could help others:
Right click on your project name
Select and expand the correct (e.g C/C++) compiler
Go to "Includes"
On right hand corner, you would see a small icon to "add path" to include your custom library
Add, build and happy compiling