Maven Thrift Plugin - maven

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.

Related

Spring + angularjs2 + war

I want to create web application with spring using maven and angularjs2 and want to create war file at the end to deploy.
When we install angularjs2 and angular-material2, node_modules itself takes 175mb. Due to which war file will get heavy. Is there a way to minimize its size or to skip files which are not required. I thought of keeping only js files and removing all ts files from node_modules but in each folder of angularjs, there are more than one js file. One file is common in all folder which is index.js and this file also depends on many other files. Now I'm not getting which all js files to keep and which all are need to remove. Is there any tool or script available which can do this for me?
Is there any other way of achieving this?
You can configure maven war plugin to exclude node directory
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<packagingExcludes>node_modules/packagingExcludes>
</configuration>
</plugin>
Also I would suggest to use grunt or any task manager to copy your important js file to another folder so that you can exclude the whole node_modules forlder.

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

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.

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>

Is Maven really flexible?

I understand how Maven works with .java files in src/java/main. But may it be used for a more general case? Let us put it more abstract: Suppose I already have some a.exe that reads some (not necessarily only .java) sources from directories A1, A2, A3 and puts some files (maybe some are generated .java) to directories B1, B2. I also have some b.exe that currently reads files from B1, B2, B3 and generates something else. Some more similar steps. (A real life problem stands behind).
I would like to write a POM.xml file so that maven will do this work. Is that possible? I assume that a.exe and b.exe should be warped as maven plugings.
Next, in Maven docs I see :
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
...
</build>
What bothers me is that "sourceDirectory" looks by itself as a hard coded name. Will Maven accept A1 and A2 tags instead?
(Will you consider accepting some of your previous questions, so that other people will be more willing to answer you?)
Maven stress for Convention over Configuration. You can treat sourceDirectory as one of the conventions, which we have predefined some elements to use with the (Java) compiler plugin.
What you want to do can be achieved. You can write a plugin/MOJO yourself, which read from whatever directory you want and invoke whatever external exe you want. By having reasonable default values in your MOJO, you can have your project POM look something like
<project>
:
:
<build>
<plugins>
<plugin>
<groupId>yourPluginGroup</group>
<artifactId>generate-A-data</artifactId>
<!-- if you want extra configuration, you can have <configuration> elements -->
</plugin>
<plugin>
<groupId>yourPluginGroup</group>
<artifactId>generate-B-data</artifactId>
</plugin>
</plugins>
</build>
<project>
Not that bad huh?
However, it is quite questionable to say whether Maven is "flexible". I believe flexibility is never the aim of Maven. Maven is specialized for building, it defines different skeletons for you to follow (e.g. build phases, dependency scopes), you can never make Maven as "flexible" as other script-based tools like Ant.
The answer is: NO, MAVEN IS NOT FLEXIBLE. Here is a simple task I could not solve even with the help from all the maven forums of this world: Write a POM that
compiles some directory full of Java files
does something else (if you please - executes a sample Hello World Mojo plugin)
compiles some other directory with some other Java files.
NO WAY !
You may be able to do this without writing plugins, by binding the exec plugin and the compiler plugin to the 'generate-sources' or 'process-classes' phases. The build helper plugin might also be useful for adding source paths.
For the original case, you could try the following bindings:
build-helper-maven-plugin bound to the initialize phase, adding any necessary source directories (eg B1 and B2)
exec-maven-plugin bound to the generate-sources phase, with two executions, one for a.exe and one for b.exe
For the Hello World Mojo case, what if you add the following to the process-classes phase (in order)?
Hello World Mojo plugin
Build Helper plugin to add source directory
Compiler plugin
The only downside I can see is that the first source directory will be recompiled. But the compiler should be smart enough to know that nothing has changed, and whether it's a problem depends on why you actually want to do this. Why do you need the class files from directory A before you run Hello World? Perhaps you're really looking at two separate pieces of code that should become separate modules?

Maven Plugin to mark empty directories

Is there something for Maven that I can use to create "placeholders" (e.g. a .empty or EMPTY) file for empty directories? Mercurial does not include empty directories so I need these directories filled hopefully via some automated way.
In the past, I used a Python script that does exactly this. I was hoping for a more Java-esque or Maven-esque approach.
Thanks
You could always use maven-antrun-plugin or gmaven-plugin to script putting a file in your directories. And then submit those empty files to your source control.
HOWEVER, this would have maven generate source code that is going to be checked in. Which is a bad idea (or at least not what you want to regular build to do).
If your source control does not keep track of empty directories, but they're needed for your build, I would recommend instead having a generate-source hook in your maven lifeycle that creates those. (Ideally, in your target directory to keep it clean, however since your source control won't keep track of them there is little harm in putting them right in your source folders if that makes your life easier).
Something like (consider that pseudo-code, absolutely not tested):
<build>
<plugins>
<plugins>
<artifactId>maven-antrun-plugin</...>
<executions>
<execution>
<phase>generate-source</phase>
<goals><goal>antrun</...
<configuration>
<tasks>
<mkdir dir="my directory1"/>
<mkdir dir="my directory2"/>

Resources