How do I specify a directory prefix on a set of resources in maven's pom.xml - maven

I have a set of files I'd like to include in the .jar generated by mvn compile. Unfortunately, I would like them to be placed in a specific path inside the .jar. For example, I want shaders/main.glsl to be in the .jar file as com/purplefrog/expglsl/castle/main.glsl
How do I specify this mapping in the pom.xml ? I can not mess with the directory heirarchy of the source project without throwing a wrench into other coders' workflows.

During the process-resources phase non-compilable files can be moved (by the maven-resources-plugin). What you should do is add a resource-block to your pom. Here you need to specify the directory. You can also add a targetPath. All together it would look like
<resource>
<directory>shaders</directory>
<!-- include all ore just a couple of files? -- >
<includes>
<include>main.glsl</include>
</includes>
<targetPath>com/purplefrog/expglsl/castle</targetPath>
</resource>
Now these files are copied to the target/classes and during the package phase they'll become part of the jar.

Take a look at the Maven Resources Plugin and this question.
Sounds like that should handle what you're looking to do if modifying the project structure up front isn't an option.

Related

Maven webapp with non-default web resource directory

Maven sets the default webapp directory to src/main/webapp as per http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html.
We use a IDE-configured server for development, which uses the files from this directory to serve. The server doesn't work from another directory and serves directly from the file system. This has the benefit that every change we make to the source files is visible instantly.
However, all the files in the webapp directory are not minified, not concatenated, etc. I have currently setup grunt to take the files from the webapp directory and put the deployment-ready resources in src/main/webapp/dist.
The problem: when building a war, the contents of src/main/webapp are copied into the war, but I want only the deployment-ready files from src/main/webapp/dist to be copied into the war.
I've tried countless google searchs for the topic and I'm feeling stupid. As already stated, I found "http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html", which says "these settings can be overridden via the project descriptor", but it does not show how. I found http://maven.apache.org/pom.html#Build_Element which doesn't show the webapp directory. I've found "http://maven.apache.org/guides/mini/guide-using-one-source-directory.html" which again, doesn't specify how to change the directories.
I know I can just specify src/main/webapp/dist as an additional resource directory and it will be copied into root war directory. But I don't want all the development files available in the production build.
Also, if someone knows of a better way of handling my general approach, I would like to hear it as well.
I found the setting, finally. http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html
Add
<warSourceDirectory>src/main/webapp/dist</warSourceDirectory>
to the maven-war-plugin configuration, like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>src/main/webapp/dist</warSourceDirectory>
</configuration>
</plugin>

maven is src/main/resources always picked up?

I have a custom properties folder for each build environment. I have defined profiles for it and added the different folder. They get picked up properly. However the resources in src/main/resources is not being picked up. I am using netbeans 8 if it makes a difference. My question is once a different profile is activated, should the main resources or test resources need be specified again in the profile? Thanks.
I am developing a java web project using maven. There are some xml files (handlerchain.xml) for some web services that need to be redeployed. These files reside in the same source directory as the java files which do not get copied. Hence I put these files into src/main/resources directory so that they will be deployed properly when it is time to build/test...
I also have a custom resources directory where I hold a set of properties files for each build case. They are all named the same but reside in different directories such as dev, prod, etc.. To deploy these into the build, I am using profiles and hence I have one profile set up for dev, prod, etc. In these profiles, I have added the custom resources directory with the proper directory such as custom-resources/dev for dev profile and custom-resources/prod for production profile.
I have noticed that the src/main/resources do not get deployed when I am using dev profile. It only gets deployed when I specifically add it as a resource in the profile/build/resources path. Is this because since I am using a custom profile and did override resources, I have to specify it?
In other words, I did not have to tell Maven where my java files are so why do I have to specify the directory for src/main/resources? Thanks.
Once you declare an explicit resource in the pom.xml Maven forgets that src/main/resources ever existed. You must now put it back explicitly:
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>

Maven Thrift Plugin

I am trying to automate the generation of source files from the .thrift files and later the packaging. As far as I know, the maven-thrift-plugin is restrictive in the sense that source and destination directories are fixed. Is there any way I can specify the source and destination directories? I could probably achieve this by using the maven-antrun-plugin but I don't want to pollute my pom unnecessarily if I don't have to.
Thanks.
As far as I can see from the source (https://github.com/dtrott/maven-thrift-plugin/blob/master/src/main/java/org/apache/thrift/maven/ThriftCompileMojo.java) there are configuration properties that control this behaviour.
Try these properties, they should work:
thriftSourceRoot
thriftTestSourceRoot
outputDirectory
These props should be added to the <configuration> section along with <thriftExecutable>, etc:
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.10</version>
<configuration>
<thriftExecutable>/usr/local/bin/thrift</thriftExecutable>
<thriftSourceRoot>${basedir}/src/main/my_custom_thrift_root</thriftSourceRoot>
</configuration>
<executions>
...
</plugin>
I also ended up going the maven-antrun-plugin route, here is a functional example: https://github.com/cobbzilla/cobbzilla-wizard/tree/master/wizard-thrift
pom.xml uses maven-antrun-plugin to exec the thrift target in build.xml
build.xml does the thrift compilation and packaging.
Generated sources go back into the source tree; I don't like derived files polluting my upstream source control, so the generated files are under a thrift package, and the package directory is in a .gitignore file. A bit kludgy.
A better way I learned about since writing that code is to compile multiple java source directories in a single maven project, which would be cleaner.

How to update folders automatically with IntelliJ

I have a Maven configuration that copies my web resources to a directory in target. From there it is read by Jetty. What I want (and what Eclipse always did for me) is update the target/web directory when something in the src/main/webapp directory changed. I can't get IntelliJ to do the same:
The resource configuration like this:
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>less/</exclude>
</excludes>
<filtering>false</filtering>
<targetPath>${project.build.directory}/web</targetPath>
</resource>
Right now I have to run the Generate sources and update folders everytime I make a change. Can't IntelliJ Detect this automatically?
Notes:
I do not build a war but a folder distribution.
I already tried moving it to target/generated-sources/web but that makes no difference.
The target/web is not marked as excluded in the module configuration.
The folder is marked as a resource folder. I tried marking it as a source folder but that made no difference.
The problem can be solved by using the File watchers plug-in. This plug-in doesn't ship with IntelliJ by default but it is very useful. From there, you can watch your *.less/html/js files and re-generate them if you edit them. In my case I run the appropriate Maven goals but you can also call the less compiler directly if you want to.
In the configuration set "Output paths to refresh" to the the custom directory you are using (in my case $OutputPath$/web). After that, any change should be refreshed automatically.
I think, yes: try pressing Ctrl+Shift+A, type "Import Maven", click the checkbox "Import Maven project automatically". This will enable auto-import which copies resources as well.

Manipulating __artifactId__ during file generation by maven archetype

Building a Maven archetype where files are generated using _artifactId_. archetype.xml looks like:
<sources>
<source>src/main/java/__artifactId__.java</source>
<source>src/main/java/__artifactId__CommandExecutor.java</source>
<source>src/main/java/__artifactId__EventListener.java</source>
</sources>
Generating a project using this archetype can lead to Java file names that do not follow the naming convention such as sample-plugin.java and sample-pluginCommandExecutor.java.
How can I make sure _artifactId_ is converted to appropriate Java file name, such as SamplePlugin.java and SamplePluginCommandExecutor.java.
I'm not an archetype expert, in fact I just did it once but
faced with a very similar problem.
I was not able to find a way to customize file or directory names
in any way, I mean use one of the archetype parameters and transform
it.
So there are 2 kinds of solution I used
Add a new archetype parameter where you specify the class name
Use a standard Name (like ArtifactClass.java) and then create an maven profile (enabled if this file is present) with an ant script that changes the name (just remember inside your files you can use velocity to customize it)
Hope it helps
tonio

Resources