How to edit the directory structure in Maven? - maven

I am using Maven project, when i create the Maven module of jar packaging, maven auto generates directory structue as src/main/java, src/main/resources, src/test/java and src/test/resources. Can I edit the above names as per my wish? Can I add new folders to the same parent? Also when i googled, I came to know abt super POM, can anybody suggest how to edit the same with the custom directory structure. I have configured sonatype maven to my eclipse from the link http://m2eclipse.sonatype.org/sites/m2e

Assuming you have a good reason to do this, you can rename the folders and indicate to maven what is the edited one by specifying the appropriate properties/sections in pom.xml of your project. I suppose m2e will pick up the changes once made to the pom.
The relevant section in your case would be (from the superpom)
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
If you want to add additional source folders or resources (not subfolders), then you can use build helper maven plugin. Again, not sure what m2e will do.

Related

Packing Angular 2 app into JAR using Maven

I have a Front-End application built with Angular2 + Webpack with the following structure:
> app
> config
> dist
> dll
> node_modules
> src
pom.xml
package.json
... (other files)
And I'm currently trying to use Maven to package everything generated by the application in dist (css, html, js) into a .jar.
I've filled most of my pom.xml file with some of the required tags like, license, developers, scm, groupId, artifactId, etc. What I'm currently having trouble with and would love some help with is the build part of the pom.xml where I imagine an execution or configuration should go that handles the packaging specifics like directories, but this is what I'm not too sure about, any help is greatly appreciated!
P.S. Is it possible to switch out the dist directory for META-INF/resources/webjars/?
Do you need something like this or I misunderstood you?
<build>
<resources>
<resource>
<directory>dist</directory>
<targetPath>dist</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>

How to copy directory using maven copy resoures plugin

I have referred the maven documentation here https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html to understand about including whatever the files we need. But it has mentioned only about including files. So how can I copy all the directories and files inside a specific directory using maven resources plug-in?
Try without specifying any includes and excludes so that it picks all directories and files inside the resources directory.
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

Maven project.build.directory

In Maven, what does the project.build.directory refer to? I am a bit confused, does it reference the source code directory or the target directory in the Maven project?
You can find those maven properties in the super pom.
You find the jar here:
${M2_HOME}/lib/maven-model-builder-3.0.3.jar
Open the jar with 7-zip or some other archiver (or use the jar tool).
Navigate to
org/apache/maven/model
There you'll find the pom-4.0.0.xml.
It contains all those "short cuts":
<project>
...
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
...
</build>
...
</project>
Update
After some lobbying I am adding a link to the pom-4.0.0.xml. This allows you to see the properties without opening up the local jar file.
It points to your top level output directory (which by default is target):
https://web.archive.org/web/20150527103929/http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide
EDIT: As has been pointed out, Codehaus is now sadly defunct. You can find details about these properties from Sonatype here:
http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html#resource-filtering-sect-project-properties
If you are ever trying to reference output directories in Maven, you should never use a literal value like target/classes. Instead you should use property references to refer to these directories.
project.build.sourceDirectory
project.build.scriptSourceDirectory
project.build.testSourceDirectory
project.build.outputDirectory
project.build.testOutputDirectory
project.build.directory
sourceDirectory, scriptSourceDirectory, and testSourceDirectory provide access to the source directories for the project. outputDirectory and testOutputDirectory provide access to the directories where Maven is going to put bytecode or other build output. directory refers to the directory which contains all of these output directories.
You can find the most up to date answer for the value in your project just execute the
mvn3 help:effective-pom
command and find the <build> ... <directory> tag's value in the result aka in the effective-pom. It will show the value of the Super POM unless you have overwritten.
Aside from #Verhás István answer (which I like), I was expecting a one-liner for the question:
${project.reporting.outputDirectory} resolves to target/site in your project.

Is there a way to remotely deploy EAR to Oracle AS using maven?

Is there a way to remotely deploy an EAR after building it to Oracle AS? Remote deployment functionality exists for Eclipse, and I'm looking for something similar for maven2:
http://download.oracle.com/docs/cd/E14545_01/help/oracle.eclipse.tools.weblogic.doc/html/conFeatureOverview.html#remoteDep
Verified on Oc4j 10
http://chadthedeveloper.blogspot.com/2008/05/automated-deployment-to-oc4j-in-maven2.html
Have you tried the Weblogic Maven plugin? The weblogic:deploy goal seems to do exactly what you want. I've not used it myself so can't confirm if it actually works or not.
Update:
Found this blog that describes deploying to 10.1.3, though not using the weblogic plugin.
This is not a complete answer your question; is's just a checklist for things to look up while solving this:
what to do with generated sources, when to generate them?
do you need custom information in META-INF?
how to manage jars?
you can have them installed in an external repository, local repository, or specify them with system, on a project relative path, or a system absolute path
if specified with system, you can keep compile time jars in any location, and those that you want inside wars inside webapp/WEB-INF
If your project has a maven friendly architecture, then ok. Otherwise you can specify custom paths like this:
<packaging>ear</packaging>
<build>
<finalName>ear-name</finalName>
<!-- you can have only one source path-->
<sourceDirectory>src-dir-path</sourceDirectory>
<!-- you can have only one test path-->
<testSourceDirectory>test-dir-path</testSourceDirectory>
<!-- you can have several resource paths -->
<resources>
<resource>
<directory>src-resources-path</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<!-- you can have several test resource paths -->
<testResources>
<testResource>
<directory>test-resources-path</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
Other tips:
study Eclipse generated ear, and maven generated ear, check for differences
you can find missing jars with mvn compile, don't use an IDE for this, you want to make sure that maven has all the jars it needs
you can have ant scripts inside maven, use maven-antrun-plugin, i can provide examples if requested

Where should I put application configuration files for a Maven project?

I'm using the Maven Application Assembler plugin to generate stand-alone executables from my Java project. The application reads in configuration files, including Spring files. The Application Assembler plugin has an option (activated by default) to add a etc/ directory to the application's classpath, but what should I do to have the plugin copy my configuration files to this directory?
Or more generally, where is in Maven the kosher location for application configuration files that should NOT be packaged in the artifact?
You can also use resource filtering:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files
turn on filtering:
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
...
make a file under src/main/resources like: application.properties
application.properties
configprop.1=${param1}
configprop.2=${param2}
Then setup a profile and set some properties perhaps in a settings.xml
that sets different properties depending on if this is a dev or production build.
see: http://maven.apache.org/guides/introduction/introduction-to-profiles.html
I have different properties set depending on if this is the build server, dev or a production deployment
mvn -Denv=dev || mvn -Denv=dev-build || mvn -Denv=production
The maven link has a pretty good description.
For folks who have come to this more recently there is, since version 1.1 of the Application Assembler Plugin, the optional parameters configurationSourceDirectory and copyConfigurationDirectory. Please find them in an example POM.xml extract below:
<configuration>
<!-- Set the target configuration directory to be used in the bin scripts -->
<configurationDirectory>conf</configurationDirectory>
<!-- Copy the contents from "/src/main/config" to the target
configuration directory in the assembled application -->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- Include the target configuration directory in the beginning of
the classpath declaration in the bin scripts -->
<includeConfigurationDirectoryInClasspath>
true
</includeConfigurationDirectoryInClasspath>
...
</configuration>
More information is here
You could try the maven assembly plugin. I used it in conjunction with the appassembler plugin.
Configure appassembler to point to whatever name you want for your configuration directory, if you don't want 'etc'. The assembly plugin assembles everything in its own output directory, so I configure the assembly plugin to copy the bin and repo dirs from the appassembler directory into its output dir, then I have it copy the config files (mine are in src/main/config) into the expected config dir. There is some duplication in this, because you are copying the appassembler outputs, but that didn't really bother me.
So what you have after executing the assembly plugin is your bin, repo, and config dir are all peer directories under the assembly output directory. You can configure it to have a different structure if you prefer, I just wanted mine to mirror the appassembler structure.
The nice thing is that you can also configure the assembly plugin to change your binaries to executables, which I could't see how to do with appassembler. And, if you then bind appassembler:assemble and assembly:single goals to the package phase, all you have to do is 'mvn package', and it assembles everything.
I don't know if I understand you correctly. But what I have done in the past for a project where I needed to copy configuration files, is use the Maven AntRun plugin. What I did is execute the plugin in the process-resources phase and copied my configuration files to the specified directory using the Ant copy task. The Assembler plugin executes in the package phase so it should pick up your configuration files if you put it in the right place. Hope this answers your question a little bit.
I had been looking for an answer to what I think is your question, or at least a very similar question. Maven allows you to specify directories for resources using the maven-resources-plugin. I have a few configuration files in one of my resource directories. I've noticed that by putting copies of those files in the etc/ directory that you mention (which is at the beginning of my CLASSPATH) I can change values in those files for use at run time. I then wanted to have that etc/ directory created with copies of everything from my resource directory by default. The copy-resources goal from the maven-resources-plugin allowed me to do that. This stanza from Examples > Copy Resources on the left sidebar (I'm limited to 2 links in this post) is what did it for me:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>

Resources