File: html2js.md

package info (click to toggle)
angular-maven-plugin 0.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 140 kB
  • sloc: java: 593; xml: 105; makefile: 2
file content (90 lines) | stat: -rw-r--r-- 3,725 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
html2js
=======

A maven plugin goal that mimics grunt-html2js in combining html templates into a single javascript file for use with Angular.js. It does NOT use grunt or node.

The html2js goal allows the specification of a source directory, include and exclude file patterns, and a target. It will collect all the html from the source directory that satisfies the include/exclude requirements and convert them into javascript statements and write them to target. If you use require.js (as I do) then you can set the addRequireWrapper flag that will cause the code to be wrapped in the appropriate declare.

To use the plugin add the following to the pom of the project containing the templates...

	<build>
		<plugins>
			<plugin>
				<groupId>com.keithbranton.mojo</groupId>
				<artifactId>angular-maven-plugin</artifactId>
				<version>0.3.4</version>
				<executions>
					<execution>
						<phase>generate-resources</phase>
						<goals>
							<goal>html2js</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<sourceDir>${basedir}/src/main/template/</sourceDir>
					<angularDependency>angular</angularDependency>
					<include>**/*.html</include>
					<exclude>not-this.html</exclude>
					<target>${basedir}/src/main/generated/js/templates.js</target>
					<addRequireWrapper>true</addRequireWrapper>
					<prefix>/templateCachePrefix</prefix>
				</configuration>
			</plugin>
		  ...

The above shows examples of the configuration parameters available. The values shown for sourceDir, include and target are the default values provided by the plugin. By default there is no exclude or prefix and addRequireWrapper is false.

Using html2js with Eclipse (kepler)
-----------------------------------

As I work I like the template.js file to be updated whenever I add/change/delete a template. The plugin is designed to be incremental build aware. To use it add the following section to your pom...

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>com.keithbranton.mojo</groupId>
										<artifactId>angular-maven-plugin</artifactId>
										<versionRange>[0.1-SNAPSHOT,)</versionRange>
										<goals>
											<goal>html2js</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<execute>
											<runOnIncremental>true</runOnIncremental>
										</execute>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
				...

With that in place you should be able to make a change to a template and see the result in your target file as soon as you save the change.

The plugin adds some info to the maven log, which can be seen in the eclipse console if you choose the maven console. This can be helpful in diagnosing misconfiguration-type problems.

Changes
-------

Feel free to fork if you'd rather take the plugin in a different direction. If you'd like to send me pull requests for improvements that you think could benefit the community I'd be happy to consider them.

Thanks
------

Thanks to jkorri for adding the optional <prefix> configuration option, which adds the supplied prefix to the start of each template name in the template cache.

Thanks to cybercomkvint for reporting and supplying a pull request to correct a windows incompatibility.

Thanks to yexela for submitting a pull request to allow the name of the angular dependency to be configured.