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

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

Related

Creating second customized POM in eclipse

Within a maven project under eclipse, I want to have a second(or customized) pom.xml in which I can use plugings like the assembly-plugin.
The problem with this plugin is that it requieres an outputh path which is only interesting for me.
Since I'm using git to push to a remote repository, I don't want to pollute the version controlled pom.xml with my private paths and other stuff.
I read about inheritance and multi-mode possibilities, but I only need two poms:
1) One for the public with general settings
2) One only for me with cusotimzed build options
I tried to create a second pom file and wanted to build the project with a new run configuration, but I don't know how to pass the -f parameter(which should call a different pom) in that dialog.
Thanks for hints or best practices.
Example of what I want to put in the custom pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>some\private\path</outputDirectory>
<finalName>SomeName</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
Select the second POM in the Package Explorer, right click -> Run As -> Maven Build...
That should run Maven with the custom POM.
If you don't get the Maven options in the "Run As" menu, go to the "Content Types" preferences page -> Text -> XML -> Maven POM XML.
Add the name of your custom POM so Eclipse understands that this is also a POM (I'm not 100% sure it will look inside a file to determine the type).
If that also fails, you can use a trick: Write a small tool that takes the unmodified POM, adds the XML which you need and then runs Maven. On Linux, you can use shell scripts for that. On Windows, a small Java program might be easier. Or have a look at PowerShell.

Force unpacking with maven-dependency-plugin

Im am using Maven 3.2.1 with Eclipse Kepler. I have a dynamic web project which has a dependency to another component which includes some JSPs. I now want the JSPs from the dependency to become part of the web root of the dynamic web project. I chose to accomplish this by using unpack goal of the maven-dependency-plugin.
I added a plugin definition to unpack the JARs into /target/m2e-wtp/web-resources. Unfortunatley Eclipse from time to time cleans this folder and teh JSPs are gone. In order to unpack them again I have to delete the target/dependency-maven-plugin-markersfolder. Otherwise the plugin will not unpack the files again.
Is it possible to force the unpacking and ignore the plugin markers?
Is there a better way to get web resources from a dependency into my Dynamic Web Project?
Yes there's a better way, but it's not compatible with Tomcat's "serve module without publishing" feature (or Weblogic's equivalent thing)
Remove your maven-dependency-plugin configuration and add your dependency as a war overlay instead. If your dependency is a war, it'll be automatically recognized as an overlay (http://maven.apache.org/plugins/maven-war-plugin/overlays.html). If it's a zip or a jar, you need to add a specific configuration to your maven-war-plugin definition. Something along :
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<overlays>
<overlay>
<!-- /!\ must also be added as a project dependency-->
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<type>jar</type>
<targetPath>relative/path/to/contextroot</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
Before deployment, resources will be unzipped under target/m2e-wtp/overlays/bar-version.jar/ and then deployed onto your application server

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!

Add classpath to folder containing property files in Maven

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".