Spring + angularjs2 + war - spring

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.

Related

Maven - Remove Generated Folders

I'm using the maven-compiler plugin to generate my .jar
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
When I do a clean install it will generate folders such as "generated-sources", "maven-archiver", "maven-status" amd "classes" in addition to my .jar
How could I automatically deleted those folders after the install or prevent them from being generated?
You cannot prevent those folders from being generated as they are essential to making the build work. generated-sources most likely contains Java source code that was generated during the build and is needed to make the rest of the code compile; classes contains the compiled Java source code that was under src/main/java and is needed to make a subsequent JAR or WAR, etc. So, without those folders, the build cannot properly work.
However, they are inherently temporary. In fact, the whole target folder is temporary. It contains data that is generated / copied at build-time and is needed to make the final artifacts. This is why it is generally a good idea to always clean before building a Maven project: it makes sure that this build folder is cleaned so that new fresh data is created (otherwise, it might rely on old build data, potentially making hard to track down bugs).
Once the final artifacts are created, they will be the only one to be considered when installing or deploying the project. If you really want to get rid of those files after the build (but I don't see why), you could always run mvn clean install clean. This will delete the target folder once the project's artifacts are installed.

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>

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.

Resources