Guide on setting up an Angular 2 project with Typescript using Maven

As a beginner in Angular2 development, my project's tech stack consists of Angular2 with TypeScript on the frontend and Spring on the backend. I have decided not to utilize a node server for compiling my frontend, but rather I plan to use TOMCAT and Maven. I have a few inquiries:

  1. My understanding is that the node server generates .js and .js.map files for each .ts file, as browsers only recognize .js files. How can I achieve this using Maven and Tomcat?
  2. I would like to construct my application from the ground up with Maven, and I would prefer to use bower as the frontend task manager.

Could someone provide a detailed guide on creating an Angular2 + Spring application utilizing 'bower or alternative frontend task management tools for tasks such as file minification and app scaffold creation,' along with 'Maven for backend task management'? I am open to any suggestions.

Answer №1

My Angular 2 + Spring Boot application uses typescript .ts files, with maven handling dependencies through npm install and file conversion via npm run tsc using the exec-maven-plugin.

Below is the section of my pom.xml file where the plugins are configured to run npm tasks from the src/main/resources path.

pom.xml

<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.3.5.RELEASE</version>
</parent>

<packaging>war</packaging>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>exec-npm-install</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>
                        <executable>npm</executable>
                        <arguments>
                            <argument>install</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
                <execution>
                    <id>exec-npm-run-tsc</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>
                        <executable>npm</executable>
                        <arguments>
                            <argument>run</argument>
                            <argument>tsc</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The project folder structure consists of Angular2 + Spring Boot application files as follows:

src/main/resources
                  /app          - .ts and .js files
                  /css
                  /images
                  /js           - systemjs.config.js included
                  /node_modules - generated by npm install and included in war
                  /typings
                  application.properties
                  package.json
                  tsconfig.json
                  typings.json

src/main/webapp
               /WEB-INF
                       /jsp     - contains .jsp files

The systemjs.config.js is included in the head section of the .jsp files.

<script type="text/javascript" src="webjars/zone.js/0.6.12/dist/zone.js"></script>
<script type="text/javascript" src="webjars/reflect-metadata/0.1.3/Reflect.js"></script>
<script type="text/javascript" src="webjars/systemjs/0.19.27/dist/system.js"></script>
<script type="text/javascript" src="js/systemjs.config.js"></script>

The WebMvcConfigurerAdapter code is used to map paths within the application.

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.my.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

    // Resource handlers configuration

    // InternalResourceViewResolver configuration

}

Additional tweaks were required to run the exec-maven-plugin on Eclipse for Windows or Mac OS. For Windows, a node.bat and npm.bat file were added, while for Mac, symbolic links for node and npm were created.

Answer №2

When it comes to typescript files, denoted by the .ts extension, they undergo compilation with the typescript compiler, rather than node.js. These two entities are completely separate. For more information on typescript itself, visit http://www.typescriptlang.org/.

While Angular2 can be utilized without typescript by writing plain old Javascript, the Angular2 team relies on Typescript for constructing the framework.

Therefore, in response to your initial inquiry, neither plays a role. You have the freedom to create your HTML, CSS, and Javascript in whatever manner you see fit.

Regarding the use of bower, it is important to note that Angular2 is not officially available on bower, only on npm. However, if you are adamant about using bower, you can refer to the discussion on GitHub at https://github.com/angular/angular/issues/4018. In the discussion, it is mentioned that you can utilize the GitHub endpoint if you prefer to employ bower.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating components with SCSS files in an Angular library can be achieved by following a few simple

After creating an scss app and associating a library, I encountered an issue when trying to create components with .scss files inside the library. Angular automatically assigned a css file to the component created within the library. The application (name ...

Setting the [required] attribute dynamically on mat-select in Angular 6

I'm working on an Angular v6 app where I need to display a drop-down and make it required based on a boolean value that is set by a checkbox. Here's a snippet of the template code (initially, includeModelVersion is set to false): <mat-checkbo ...

Discovering the right category for a general component: A step-by-step guide

How about creating a new list component that can work with an array of objects? <script setup lang="ts"> const props = defineProps<{ items: Record<string, unknown>[], selected: Record<string, unknown> | null field: stri ...

How to properly pass a parameter value to an Angular service when using inject.get in the constructor

After experimenting with injecting services in Angular, I encountered an issue when trying to call LogService within GlobalErrorHandler. Despite my best efforts, the code below just wouldn't work. I discovered that the problem stemmed from not passin ...

Nested self-referencing in Typescript involves a structure where

Please note that the code below has been simplified to highlight a specific issue. The explanation before the code may be lengthy, but it is necessary for clarity. Imagine I have a Foo class that represents a complex object. interface Config { bars:{ ...

React: Issue with passing arguments to redux action hooks

In my React application, I have implemented Redux-Toolkit to manage reducers and actions with slices. I am currently working on creating actions that can update and delete values from the store, requiring arguments for their usage. To achieve this, I have ...

Error Alert: missing property data in angular 5 type

I attempted to design an interface in interface.ts. The data consists of an array of objects inside the Column. Below is the code for my interface: export class User { result: boolean; messages: string; Column=[]; data=[]; } export class Column { name ...

Applying ORM Drizzle in cases of conflict

Here's where I'm currently at: If I use onConflictDoNothing, the plan is to insert a new record into the database. However, if a record with the same userId and provider already exists, and the apiKey of the existing record is not equal to the ap ...

An unexpected error has occurred in Angular 2 while referencing the service proxy with code asd:18. The error message is: (SystemJS) Unexpected token < SyntaxError: Unexpected token

Forgive me for asking what may seem like a silly question. I am a newcomer to Angular 2 and encountering some problems with the API that my app is utilizing. The consumed Web API is located within the following folder structure: - src - app - Regist ...

The resend email feature isn't functioning properly on the production environment with next js, however, it works seamlessly in the development environment

import { EmailTemplate } from "@/components/email-template"; import { Resend } from "resend"; const resend = new Resend("myApiKey"); // this works only in dev // const resend = new Resend(process.env.NEXT_PUBLIC_RESEND_API_KE ...

Mocking a service dependency in Angular using Jest and Spectator during testing of a different

I am currently using: Angular CLI: 10.2.3 Node: 12.22.1 Everything is working fine with the project build and execution. I am now focusing on adding tests using Jest and Spectator. Specifically, I'm attempting to test a basic service where I can mo ...

Invoke a function and assign it to an export variable

I have a query regarding a file containing an export constant that is utilized to construct a navigation bar in CoreUI. However, I am exploring methods to generate dynamic JSON data within other Components or the same file and then inject it into the exp ...

Is it necessary to create a wrapper for Angular Material2 components?

I have multiple angular 5 projects in progress and my team is considering incorporating material design components from https://material.angular.io/. Would it be beneficial to create a wrapper layer to contain the material design components? This would me ...

Is there a way to make the components declared in entryComponents accessible to other modules for exporting?

Is it possible to dynamically create a component in app.module when the component is declared in the entryComponents section of app.module? // page.module.ts @NgModule({ declarations: [ ... DynamicComponent ], **entryCompone ...

When trying to access the "form" property of a form ElementRef, TypeScript throws an error

I've encountered an issue with accessing the validity of a form in my template: <form #heroForm="ngForm" (ngSubmit)="onSubmit()"> After adding it as a ViewChild in the controller: @ViewChild('heroForm') heroForm: ElementRef; Trying ...

Issue with Ionic Native File: File.writeFile function - file is not being created and there is no callback response

I've exhausted all the different solutions I could find, but unfortunately, the file isn't getting saved and nothing seems to be happening. The callback functions aren't being called - neither success nor error. Here are the solutions I&apo ...

What is the best way to include a router-link in a button click event in Angular 8?

Can someone please help me with adding a routing function to a button in Angular? I have already included a (click) function on the button, but how do I actually make the function navigate within the home.ts component? <button class="navbut" (click)= ...

Retrieving information from a data file by implementing a GraphQL Apollo Server within a NextJS application route

Currently working with Next.js 14 (app route), React, and the GraphQL Apollo framework. I have a JSON file containing data saved locally that I'd like to display using the server API. How can I make this happen? Below is the JSON structure I need to r ...

The process of uploading an Angular application to a server with an altered baseHref, resulting in the

I recently posted a question and had some discussions in the comments. Here is the link to the previous post for reference: Angular base href not showing up in URL In summary, my application works fine locally, with correct routing between localhost:4200/ ...

Update a component using a different component

Hello there, I am currently working with Angular 7 and wanted to share the component's structure: https://i.sstatic.net/EvmOi.png At the moment, I am passing data from Comp_3 to Comp_2 in order to update the 'Message' field. However, this ...